1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2021 Google LLC.
4  *
5  * Common part of most Semtech SAR sensor.
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/bitops.h>
10 #include <linux/byteorder/generic.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/interrupt.h>
16 #include <linux/irqreturn.h>
17 #include <linux/i2c.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <vdso/bits.h>
23 
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/events.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/triggered_buffer.h>
29 #include <linux/iio/trigger_consumer.h>
30 
31 #include "sx_common.h"
32 
33 /* All Semtech SAR sensors have IRQ bit in the same order. */
34 #define   SX_COMMON_CONVDONE_IRQ			BIT(0)
35 #define   SX_COMMON_FAR_IRQ				BIT(2)
36 #define   SX_COMMON_CLOSE_IRQ				BIT(3)
37 
38 const struct iio_event_spec sx_common_events[3] = {
39 	{
40 		.type = IIO_EV_TYPE_THRESH,
41 		.dir = IIO_EV_DIR_RISING,
42 		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
43 	},
44 	{
45 		.type = IIO_EV_TYPE_THRESH,
46 		.dir = IIO_EV_DIR_FALLING,
47 		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
48 	},
49 	{
50 		.type = IIO_EV_TYPE_THRESH,
51 		.dir = IIO_EV_DIR_EITHER,
52 		.mask_separate = BIT(IIO_EV_INFO_ENABLE) |
53 				 BIT(IIO_EV_INFO_HYSTERESIS) |
54 				 BIT(IIO_EV_INFO_VALUE),
55 	},
56 };
57 EXPORT_SYMBOL_NS_GPL(sx_common_events, SEMTECH_PROX);
58 
59 static irqreturn_t sx_common_irq_handler(int irq, void *private)
60 {
61 	struct iio_dev *indio_dev = private;
62 	struct sx_common_data *data = iio_priv(indio_dev);
63 
64 	if (data->trigger_enabled)
65 		iio_trigger_poll(data->trig);
66 
67 	/*
68 	 * Even if no event is enabled, we need to wake the thread to clear the
69 	 * interrupt state by reading SX_COMMON_REG_IRQ_SRC.
70 	 * It is not possible to do that here because regmap_read takes a mutex.
71 	 */
72 	return IRQ_WAKE_THREAD;
73 }
74 
75 static void sx_common_push_events(struct iio_dev *indio_dev)
76 {
77 	int ret;
78 	unsigned int val, chan;
79 	struct sx_common_data *data = iio_priv(indio_dev);
80 	s64 timestamp = iio_get_time_ns(indio_dev);
81 	unsigned long prox_changed;
82 
83 	/* Read proximity state on all channels */
84 	ret = regmap_read(data->regmap, data->chip_info->reg_stat, &val);
85 	if (ret) {
86 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
87 		return;
88 	}
89 
90 	val >>= data->chip_info->stat_offset;
91 
92 	/*
93 	 * Only iterate over channels with changes on proximity status that have
94 	 * events enabled.
95 	 */
96 	prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;
97 
98 	for_each_set_bit(chan, &prox_changed, data->chip_info->num_channels) {
99 		int dir;
100 		u64 ev;
101 
102 		dir = (val & BIT(chan)) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
103 		ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
104 					  IIO_EV_TYPE_THRESH, dir);
105 
106 		iio_push_event(indio_dev, ev, timestamp);
107 	}
108 	data->chan_prox_stat = val;
109 }
110 
111 static int sx_common_enable_irq(struct sx_common_data *data, unsigned int irq)
112 {
113 	if (!data->client->irq)
114 		return 0;
115 	return regmap_update_bits(data->regmap, data->chip_info->reg_irq_msk,
116 				  irq << data->chip_info->irq_msk_offset,
117 				  irq << data->chip_info->irq_msk_offset);
118 }
119 
120 static int sx_common_disable_irq(struct sx_common_data *data, unsigned int irq)
121 {
122 	if (!data->client->irq)
123 		return 0;
124 	return regmap_update_bits(data->regmap, data->chip_info->reg_irq_msk,
125 				  irq << data->chip_info->irq_msk_offset, 0);
126 }
127 
128 static int sx_common_update_chan_en(struct sx_common_data *data,
129 				    unsigned long chan_read,
130 				    unsigned long chan_event)
131 {
132 	int ret;
133 	unsigned long channels = chan_read | chan_event;
134 
135 	if ((data->chan_read | data->chan_event) != channels) {
136 		ret = regmap_update_bits(data->regmap,
137 					 data->chip_info->reg_enable_chan,
138 					 data->chip_info->mask_enable_chan,
139 					 channels);
140 		if (ret)
141 			return ret;
142 	}
143 	data->chan_read = chan_read;
144 	data->chan_event = chan_event;
145 	return 0;
146 }
147 
148 static int sx_common_get_read_channel(struct sx_common_data *data, int channel)
149 {
150 	return sx_common_update_chan_en(data, data->chan_read | BIT(channel),
151 				     data->chan_event);
152 }
153 
154 static int sx_common_put_read_channel(struct sx_common_data *data, int channel)
155 {
156 	return sx_common_update_chan_en(data, data->chan_read & ~BIT(channel),
157 				     data->chan_event);
158 }
159 
160 static int sx_common_get_event_channel(struct sx_common_data *data, int channel)
161 {
162 	return sx_common_update_chan_en(data, data->chan_read,
163 				     data->chan_event | BIT(channel));
164 }
165 
166 static int sx_common_put_event_channel(struct sx_common_data *data, int channel)
167 {
168 	return sx_common_update_chan_en(data, data->chan_read,
169 				     data->chan_event & ~BIT(channel));
170 }
171 
172 /**
173  * sx_common_read_proximity() - Read raw proximity value.
174  * @data:	Internal data
175  * @chan:	Channel to read
176  * @val:	pointer to return read value.
177  *
178  * Request a conversion, wait for the sensor to be ready and
179  * return the raw proximity value.
180  */
181 int sx_common_read_proximity(struct sx_common_data *data,
182 			     const struct iio_chan_spec *chan, int *val)
183 {
184 	int ret;
185 	__be16 rawval;
186 
187 	mutex_lock(&data->mutex);
188 
189 	ret = sx_common_get_read_channel(data, chan->channel);
190 	if (ret)
191 		goto out;
192 
193 	ret = sx_common_enable_irq(data, SX_COMMON_CONVDONE_IRQ);
194 	if (ret)
195 		goto out_put_channel;
196 
197 	mutex_unlock(&data->mutex);
198 
199 	if (data->client->irq) {
200 		ret = wait_for_completion_interruptible(&data->completion);
201 		reinit_completion(&data->completion);
202 	} else {
203 		ret = data->chip_info->ops.wait_for_sample(data);
204 	}
205 
206 	mutex_lock(&data->mutex);
207 
208 	if (ret)
209 		goto out_disable_irq;
210 
211 	ret = data->chip_info->ops.read_prox_data(data, chan, &rawval);
212 	if (ret)
213 		goto out_disable_irq;
214 
215 	*val = sign_extend32(be16_to_cpu(rawval), chan->scan_type.realbits - 1);
216 
217 	ret = sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
218 	if (ret)
219 		goto out_put_channel;
220 
221 	ret = sx_common_put_read_channel(data, chan->channel);
222 	if (ret)
223 		goto out;
224 
225 	mutex_unlock(&data->mutex);
226 
227 	return IIO_VAL_INT;
228 
229 out_disable_irq:
230 	sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
231 out_put_channel:
232 	sx_common_put_read_channel(data, chan->channel);
233 out:
234 	mutex_unlock(&data->mutex);
235 
236 	return ret;
237 }
238 EXPORT_SYMBOL_NS_GPL(sx_common_read_proximity, SEMTECH_PROX);
239 
240 /**
241  * sx_common_read_event_config() - Configure event setting.
242  * @indio_dev:	iio device object
243  * @chan:	Channel to read
244  * @type:	Type of event (unused)
245  * @dir:	Direction of event (unused)
246  *
247  * return if the given channel is used for event gathering.
248  */
249 int sx_common_read_event_config(struct iio_dev *indio_dev,
250 				const struct iio_chan_spec *chan,
251 				enum iio_event_type type,
252 				enum iio_event_direction dir)
253 {
254 	struct sx_common_data *data = iio_priv(indio_dev);
255 
256 	return !!(data->chan_event & BIT(chan->channel));
257 }
258 EXPORT_SYMBOL_NS_GPL(sx_common_read_event_config, SEMTECH_PROX);
259 
260 /**
261  * sx_common_write_event_config() - Configure event setting.
262  * @indio_dev:	iio device object
263  * @chan:	Channel to enable
264  * @type:	Type of event (unused)
265  * @dir:	Direction of event (unused)
266  * @state:	State of the event.
267  *
268  * Enable/Disable event on a given channel.
269  */
270 int sx_common_write_event_config(struct iio_dev *indio_dev,
271 				 const struct iio_chan_spec *chan,
272 				 enum iio_event_type type,
273 				 enum iio_event_direction dir, int state)
274 {
275 	struct sx_common_data *data = iio_priv(indio_dev);
276 	unsigned int eventirq = SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ;
277 	int ret;
278 
279 	/* If the state hasn't changed, there's nothing to do. */
280 	if (!!(data->chan_event & BIT(chan->channel)) == state)
281 		return 0;
282 
283 	mutex_lock(&data->mutex);
284 	if (state) {
285 		ret = sx_common_get_event_channel(data, chan->channel);
286 		if (ret)
287 			goto out_unlock;
288 		if (!(data->chan_event & ~BIT(chan->channel))) {
289 			ret = sx_common_enable_irq(data, eventirq);
290 			if (ret)
291 				sx_common_put_event_channel(data, chan->channel);
292 		}
293 	} else {
294 		ret = sx_common_put_event_channel(data, chan->channel);
295 		if (ret)
296 			goto out_unlock;
297 		if (!data->chan_event) {
298 			ret = sx_common_disable_irq(data, eventirq);
299 			if (ret)
300 				sx_common_get_event_channel(data, chan->channel);
301 		}
302 	}
303 
304 out_unlock:
305 	mutex_unlock(&data->mutex);
306 	return ret;
307 }
308 EXPORT_SYMBOL_NS_GPL(sx_common_write_event_config, SEMTECH_PROX);
309 
310 static int sx_common_set_trigger_state(struct iio_trigger *trig, bool state)
311 {
312 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
313 	struct sx_common_data *data = iio_priv(indio_dev);
314 	int ret = 0;
315 
316 	mutex_lock(&data->mutex);
317 
318 	if (state)
319 		ret = sx_common_enable_irq(data, SX_COMMON_CONVDONE_IRQ);
320 	else if (!data->chan_read)
321 		ret = sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
322 	if (ret)
323 		goto out;
324 
325 	data->trigger_enabled = state;
326 
327 out:
328 	mutex_unlock(&data->mutex);
329 
330 	return ret;
331 }
332 
333 static const struct iio_trigger_ops sx_common_trigger_ops = {
334 	.set_trigger_state = sx_common_set_trigger_state,
335 };
336 
337 static irqreturn_t sx_common_irq_thread_handler(int irq, void *private)
338 {
339 	struct iio_dev *indio_dev = private;
340 	struct sx_common_data *data = iio_priv(indio_dev);
341 	int ret;
342 	unsigned int val;
343 
344 	mutex_lock(&data->mutex);
345 
346 	ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val);
347 	if (ret) {
348 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
349 		goto out;
350 	}
351 
352 	if (val & ((SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ) << data->chip_info->irq_msk_offset))
353 		sx_common_push_events(indio_dev);
354 
355 	if (val & (SX_COMMON_CONVDONE_IRQ << data->chip_info->irq_msk_offset))
356 		complete(&data->completion);
357 
358 out:
359 	mutex_unlock(&data->mutex);
360 
361 	return IRQ_HANDLED;
362 }
363 
364 static irqreturn_t sx_common_trigger_handler(int irq, void *private)
365 {
366 	struct iio_poll_func *pf = private;
367 	struct iio_dev *indio_dev = pf->indio_dev;
368 	struct sx_common_data *data = iio_priv(indio_dev);
369 	__be16 val;
370 	int bit, ret, i = 0;
371 
372 	mutex_lock(&data->mutex);
373 
374 	for_each_set_bit(bit, indio_dev->active_scan_mask,
375 			 indio_dev->masklength) {
376 		ret = data->chip_info->ops.read_prox_data(data,
377 						     &indio_dev->channels[bit],
378 						     &val);
379 		if (ret)
380 			goto out;
381 
382 		data->buffer.channels[i++] = val;
383 	}
384 
385 	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
386 					   pf->timestamp);
387 
388 out:
389 	mutex_unlock(&data->mutex);
390 
391 	iio_trigger_notify_done(indio_dev->trig);
392 
393 	return IRQ_HANDLED;
394 }
395 
396 static int sx_common_buffer_preenable(struct iio_dev *indio_dev)
397 {
398 	struct sx_common_data *data = iio_priv(indio_dev);
399 	unsigned long channels = 0;
400 	int bit, ret;
401 
402 	mutex_lock(&data->mutex);
403 	for_each_set_bit(bit, indio_dev->active_scan_mask,
404 			 indio_dev->masklength)
405 		__set_bit(indio_dev->channels[bit].channel, &channels);
406 
407 	ret = sx_common_update_chan_en(data, channels, data->chan_event);
408 	mutex_unlock(&data->mutex);
409 	return ret;
410 }
411 
412 static int sx_common_buffer_postdisable(struct iio_dev *indio_dev)
413 {
414 	struct sx_common_data *data = iio_priv(indio_dev);
415 	int ret;
416 
417 	mutex_lock(&data->mutex);
418 	ret = sx_common_update_chan_en(data, 0, data->chan_event);
419 	mutex_unlock(&data->mutex);
420 	return ret;
421 }
422 
423 static const struct iio_buffer_setup_ops sx_common_buffer_setup_ops = {
424 	.preenable = sx_common_buffer_preenable,
425 	.postdisable = sx_common_buffer_postdisable,
426 };
427 
428 static void sx_common_regulator_disable(void *_data)
429 {
430 	struct sx_common_data *data = _data;
431 
432 	regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
433 }
434 
435 #define SX_COMMON_SOFT_RESET				0xde
436 
437 static int sx_common_init_device(struct iio_dev *indio_dev)
438 {
439 	struct sx_common_data *data = iio_priv(indio_dev);
440 	struct sx_common_reg_default tmp;
441 	const struct sx_common_reg_default *initval;
442 	int ret;
443 	unsigned int i, val;
444 
445 	ret = regmap_write(data->regmap, data->chip_info->reg_reset,
446 			   SX_COMMON_SOFT_RESET);
447 	if (ret)
448 		return ret;
449 
450 	usleep_range(1000, 2000); /* power-up time is ~1ms. */
451 
452 	/* Clear reset interrupt state by reading SX_COMMON_REG_IRQ_SRC. */
453 	ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val);
454 	if (ret)
455 		return ret;
456 
457 	/* Program defaults from constant or BIOS. */
458 	for (i = 0; i < data->chip_info->num_default_regs; i++) {
459 		initval = data->chip_info->ops.get_default_reg(&indio_dev->dev,
460 							       i, &tmp);
461 		ret = regmap_write(data->regmap, initval->reg, initval->def);
462 		if (ret)
463 			return ret;
464 	}
465 
466 	return data->chip_info->ops.init_compensation(indio_dev);
467 }
468 
469 /**
470  * sx_common_probe() - Common setup for Semtech SAR sensor
471  * @client:		I2C client object
472  * @chip_info:		Semtech sensor chip information.
473  * @regmap_config:	Sensor registers map configuration.
474  */
475 int sx_common_probe(struct i2c_client *client,
476 		    const struct sx_common_chip_info *chip_info,
477 		    const struct regmap_config *regmap_config)
478 {
479 	struct device *dev = &client->dev;
480 	struct iio_dev *indio_dev;
481 	struct sx_common_data *data;
482 	int ret;
483 
484 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
485 	if (!indio_dev)
486 		return -ENOMEM;
487 
488 	data = iio_priv(indio_dev);
489 
490 	data->chip_info = chip_info;
491 	data->client = client;
492 	data->supplies[0].supply = "vdd";
493 	data->supplies[1].supply = "svdd";
494 	mutex_init(&data->mutex);
495 	init_completion(&data->completion);
496 
497 	data->regmap = devm_regmap_init_i2c(client, regmap_config);
498 	if (IS_ERR(data->regmap))
499 		return dev_err_probe(dev, PTR_ERR(data->regmap),
500 				     "Could init register map\n");
501 
502 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
503 				      data->supplies);
504 	if (ret)
505 		return dev_err_probe(dev, ret, "Unable to get regulators\n");
506 
507 	ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
508 	if (ret)
509 		return dev_err_probe(dev, ret, "Unable to enable regulators\n");
510 
511 	/* Must wait for Tpor time after initial power up */
512 	usleep_range(1000, 1100);
513 
514 	ret = devm_add_action_or_reset(dev, sx_common_regulator_disable, data);
515 	if (ret)
516 		return dev_err_probe(dev, ret,
517 				     "Unable to register regulators deleter\n");
518 
519 	ret = data->chip_info->ops.check_whoami(dev, indio_dev);
520 	if (ret)
521 		return dev_err_probe(dev, ret, "error reading WHOAMI\n");
522 
523 	ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev));
524 	indio_dev->dev.of_node = client->dev.of_node;
525 	indio_dev->modes = INDIO_DIRECT_MODE;
526 
527 	indio_dev->channels =  data->chip_info->iio_channels;
528 	indio_dev->num_channels = data->chip_info->num_iio_channels;
529 	indio_dev->info = &data->chip_info->iio_info;
530 
531 	i2c_set_clientdata(client, indio_dev);
532 
533 	ret = sx_common_init_device(indio_dev);
534 	if (ret)
535 		return dev_err_probe(dev, ret, "Unable to initialize sensor\n");
536 
537 	if (client->irq) {
538 		ret = devm_request_threaded_irq(dev, client->irq,
539 						sx_common_irq_handler,
540 						sx_common_irq_thread_handler,
541 						IRQF_ONESHOT,
542 						"sx_event", indio_dev);
543 		if (ret)
544 			return dev_err_probe(dev, ret, "No IRQ\n");
545 
546 		data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
547 						    indio_dev->name,
548 						    iio_device_id(indio_dev));
549 		if (!data->trig)
550 			return -ENOMEM;
551 
552 		data->trig->ops = &sx_common_trigger_ops;
553 		iio_trigger_set_drvdata(data->trig, indio_dev);
554 
555 		ret = devm_iio_trigger_register(dev, data->trig);
556 		if (ret)
557 			return ret;
558 	}
559 
560 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
561 					      iio_pollfunc_store_time,
562 					      sx_common_trigger_handler,
563 					      &sx_common_buffer_setup_ops);
564 	if (ret)
565 		return ret;
566 
567 	return devm_iio_device_register(dev, indio_dev);
568 }
569 EXPORT_SYMBOL_NS_GPL(sx_common_probe, SEMTECH_PROX);
570 
571 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
572 MODULE_DESCRIPTION("Common functions and structures for Semtech sensor");
573 MODULE_LICENSE("GPL v2");
574