dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / light / iqs621-als.c
blobb2988a782bd023535737f7bdd1cb9204ff2a68e1
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Azoteq IQS621/622 Ambient Light Sensors
5 * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com>
6 */
8 #include <linux/device.h>
9 #include <linux/iio/events.h>
10 #include <linux/iio/iio.h>
11 #include <linux/kernel.h>
12 #include <linux/mfd/iqs62x.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/notifier.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
19 #define IQS621_ALS_FLAGS_LIGHT BIT(7)
20 #define IQS621_ALS_FLAGS_RANGE GENMASK(3, 0)
22 #define IQS621_ALS_UI_OUT 0x17
24 #define IQS621_ALS_THRESH_DARK 0x80
25 #define IQS621_ALS_THRESH_LIGHT 0x81
27 #define IQS622_IR_RANGE 0x15
28 #define IQS622_IR_FLAGS 0x16
29 #define IQS622_IR_FLAGS_TOUCH BIT(1)
30 #define IQS622_IR_FLAGS_PROX BIT(0)
32 #define IQS622_IR_UI_OUT 0x17
34 #define IQS622_IR_THRESH_PROX 0x91
35 #define IQS622_IR_THRESH_TOUCH 0x92
37 struct iqs621_als_private {
38 struct iqs62x_core *iqs62x;
39 struct notifier_block notifier;
40 struct mutex lock;
41 bool light_en;
42 bool range_en;
43 bool prox_en;
44 u8 als_flags;
45 u8 ir_flags_mask;
46 u8 ir_flags;
47 u8 thresh_light;
48 u8 thresh_dark;
49 u8 thresh_prox;
52 static int iqs621_als_init(struct iqs621_als_private *iqs621_als)
54 struct iqs62x_core *iqs62x = iqs621_als->iqs62x;
55 unsigned int event_mask = 0;
56 int ret;
58 switch (iqs621_als->ir_flags_mask) {
59 case IQS622_IR_FLAGS_TOUCH:
60 ret = regmap_write(iqs62x->regmap, IQS622_IR_THRESH_TOUCH,
61 iqs621_als->thresh_prox);
62 break;
64 case IQS622_IR_FLAGS_PROX:
65 ret = regmap_write(iqs62x->regmap, IQS622_IR_THRESH_PROX,
66 iqs621_als->thresh_prox);
67 break;
69 default:
70 ret = regmap_write(iqs62x->regmap, IQS621_ALS_THRESH_LIGHT,
71 iqs621_als->thresh_light);
72 if (ret)
73 return ret;
75 ret = regmap_write(iqs62x->regmap, IQS621_ALS_THRESH_DARK,
76 iqs621_als->thresh_dark);
79 if (ret)
80 return ret;
82 if (iqs621_als->light_en || iqs621_als->range_en)
83 event_mask |= iqs62x->dev_desc->als_mask;
85 if (iqs621_als->prox_en)
86 event_mask |= iqs62x->dev_desc->ir_mask;
88 return regmap_update_bits(iqs62x->regmap, IQS620_GLBL_EVENT_MASK,
89 event_mask, 0);
92 static int iqs621_als_notifier(struct notifier_block *notifier,
93 unsigned long event_flags, void *context)
95 struct iqs62x_event_data *event_data = context;
96 struct iqs621_als_private *iqs621_als;
97 struct iio_dev *indio_dev;
98 bool light_new, light_old;
99 bool prox_new, prox_old;
100 u8 range_new, range_old;
101 s64 timestamp;
102 int ret;
104 iqs621_als = container_of(notifier, struct iqs621_als_private,
105 notifier);
106 indio_dev = iio_priv_to_dev(iqs621_als);
107 timestamp = iio_get_time_ns(indio_dev);
109 mutex_lock(&iqs621_als->lock);
111 if (event_flags & BIT(IQS62X_EVENT_SYS_RESET)) {
112 ret = iqs621_als_init(iqs621_als);
113 if (ret) {
114 dev_err(indio_dev->dev.parent,
115 "Failed to re-initialize device: %d\n", ret);
116 ret = NOTIFY_BAD;
117 } else {
118 ret = NOTIFY_OK;
121 goto err_mutex;
124 if (!iqs621_als->light_en && !iqs621_als->range_en &&
125 !iqs621_als->prox_en) {
126 ret = NOTIFY_DONE;
127 goto err_mutex;
130 /* IQS621 only */
131 light_new = event_data->als_flags & IQS621_ALS_FLAGS_LIGHT;
132 light_old = iqs621_als->als_flags & IQS621_ALS_FLAGS_LIGHT;
134 if (iqs621_als->light_en && light_new && !light_old)
135 iio_push_event(indio_dev,
136 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
137 IIO_EV_TYPE_THRESH,
138 IIO_EV_DIR_RISING),
139 timestamp);
140 else if (iqs621_als->light_en && !light_new && light_old)
141 iio_push_event(indio_dev,
142 IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
143 IIO_EV_TYPE_THRESH,
144 IIO_EV_DIR_FALLING),
145 timestamp);
147 /* IQS621 and IQS622 */
148 range_new = event_data->als_flags & IQS621_ALS_FLAGS_RANGE;
149 range_old = iqs621_als->als_flags & IQS621_ALS_FLAGS_RANGE;
151 if (iqs621_als->range_en && (range_new > range_old))
152 iio_push_event(indio_dev,
153 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
154 IIO_EV_TYPE_CHANGE,
155 IIO_EV_DIR_RISING),
156 timestamp);
157 else if (iqs621_als->range_en && (range_new < range_old))
158 iio_push_event(indio_dev,
159 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
160 IIO_EV_TYPE_CHANGE,
161 IIO_EV_DIR_FALLING),
162 timestamp);
164 /* IQS622 only */
165 prox_new = event_data->ir_flags & iqs621_als->ir_flags_mask;
166 prox_old = iqs621_als->ir_flags & iqs621_als->ir_flags_mask;
168 if (iqs621_als->prox_en && prox_new && !prox_old)
169 iio_push_event(indio_dev,
170 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
171 IIO_EV_TYPE_THRESH,
172 IIO_EV_DIR_RISING),
173 timestamp);
174 else if (iqs621_als->prox_en && !prox_new && prox_old)
175 iio_push_event(indio_dev,
176 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
177 IIO_EV_TYPE_THRESH,
178 IIO_EV_DIR_FALLING),
179 timestamp);
181 iqs621_als->als_flags = event_data->als_flags;
182 iqs621_als->ir_flags = event_data->ir_flags;
183 ret = NOTIFY_OK;
185 err_mutex:
186 mutex_unlock(&iqs621_als->lock);
188 return ret;
191 static void iqs621_als_notifier_unregister(void *context)
193 struct iqs621_als_private *iqs621_als = context;
194 struct iio_dev *indio_dev = iio_priv_to_dev(iqs621_als);
195 int ret;
197 ret = blocking_notifier_chain_unregister(&iqs621_als->iqs62x->nh,
198 &iqs621_als->notifier);
199 if (ret)
200 dev_err(indio_dev->dev.parent,
201 "Failed to unregister notifier: %d\n", ret);
204 static int iqs621_als_read_raw(struct iio_dev *indio_dev,
205 struct iio_chan_spec const *chan,
206 int *val, int *val2, long mask)
208 struct iqs621_als_private *iqs621_als = iio_priv(indio_dev);
209 struct iqs62x_core *iqs62x = iqs621_als->iqs62x;
210 int ret;
211 __le16 val_buf;
213 switch (chan->type) {
214 case IIO_INTENSITY:
215 ret = regmap_read(iqs62x->regmap, chan->address, val);
216 if (ret)
217 return ret;
219 *val &= IQS621_ALS_FLAGS_RANGE;
220 return IIO_VAL_INT;
222 case IIO_PROXIMITY:
223 case IIO_LIGHT:
224 ret = regmap_raw_read(iqs62x->regmap, chan->address, &val_buf,
225 sizeof(val_buf));
226 if (ret)
227 return ret;
229 *val = le16_to_cpu(val_buf);
230 return IIO_VAL_INT;
232 default:
233 return -EINVAL;
237 static int iqs621_als_read_event_config(struct iio_dev *indio_dev,
238 const struct iio_chan_spec *chan,
239 enum iio_event_type type,
240 enum iio_event_direction dir)
242 struct iqs621_als_private *iqs621_als = iio_priv(indio_dev);
243 int ret;
245 mutex_lock(&iqs621_als->lock);
247 switch (chan->type) {
248 case IIO_LIGHT:
249 ret = iqs621_als->light_en;
250 break;
252 case IIO_INTENSITY:
253 ret = iqs621_als->range_en;
254 break;
256 case IIO_PROXIMITY:
257 ret = iqs621_als->prox_en;
258 break;
260 default:
261 ret = -EINVAL;
264 mutex_unlock(&iqs621_als->lock);
266 return ret;
269 static int iqs621_als_write_event_config(struct iio_dev *indio_dev,
270 const struct iio_chan_spec *chan,
271 enum iio_event_type type,
272 enum iio_event_direction dir,
273 int state)
275 struct iqs621_als_private *iqs621_als = iio_priv(indio_dev);
276 struct iqs62x_core *iqs62x = iqs621_als->iqs62x;
277 unsigned int val;
278 int ret;
280 mutex_lock(&iqs621_als->lock);
282 ret = regmap_read(iqs62x->regmap, iqs62x->dev_desc->als_flags, &val);
283 if (ret)
284 goto err_mutex;
285 iqs621_als->als_flags = val;
287 switch (chan->type) {
288 case IIO_LIGHT:
289 ret = regmap_update_bits(iqs62x->regmap, IQS620_GLBL_EVENT_MASK,
290 iqs62x->dev_desc->als_mask,
291 iqs621_als->range_en || state ? 0 :
292 0xFF);
293 if (!ret)
294 iqs621_als->light_en = state;
295 break;
297 case IIO_INTENSITY:
298 ret = regmap_update_bits(iqs62x->regmap, IQS620_GLBL_EVENT_MASK,
299 iqs62x->dev_desc->als_mask,
300 iqs621_als->light_en || state ? 0 :
301 0xFF);
302 if (!ret)
303 iqs621_als->range_en = state;
304 break;
306 case IIO_PROXIMITY:
307 ret = regmap_read(iqs62x->regmap, IQS622_IR_FLAGS, &val);
308 if (ret)
309 goto err_mutex;
310 iqs621_als->ir_flags = val;
312 ret = regmap_update_bits(iqs62x->regmap, IQS620_GLBL_EVENT_MASK,
313 iqs62x->dev_desc->ir_mask,
314 state ? 0 : 0xFF);
315 if (!ret)
316 iqs621_als->prox_en = state;
317 break;
319 default:
320 ret = -EINVAL;
323 err_mutex:
324 mutex_unlock(&iqs621_als->lock);
326 return ret;
329 static int iqs621_als_read_event_value(struct iio_dev *indio_dev,
330 const struct iio_chan_spec *chan,
331 enum iio_event_type type,
332 enum iio_event_direction dir,
333 enum iio_event_info info,
334 int *val, int *val2)
336 struct iqs621_als_private *iqs621_als = iio_priv(indio_dev);
337 int ret = IIO_VAL_INT;
339 mutex_lock(&iqs621_als->lock);
341 switch (dir) {
342 case IIO_EV_DIR_RISING:
343 *val = iqs621_als->thresh_light * 16;
344 break;
346 case IIO_EV_DIR_FALLING:
347 *val = iqs621_als->thresh_dark * 4;
348 break;
350 case IIO_EV_DIR_EITHER:
351 if (iqs621_als->ir_flags_mask == IQS622_IR_FLAGS_TOUCH)
352 *val = iqs621_als->thresh_prox * 4;
353 else
354 *val = iqs621_als->thresh_prox;
355 break;
357 default:
358 ret = -EINVAL;
361 mutex_unlock(&iqs621_als->lock);
363 return ret;
366 static int iqs621_als_write_event_value(struct iio_dev *indio_dev,
367 const struct iio_chan_spec *chan,
368 enum iio_event_type type,
369 enum iio_event_direction dir,
370 enum iio_event_info info,
371 int val, int val2)
373 struct iqs621_als_private *iqs621_als = iio_priv(indio_dev);
374 struct iqs62x_core *iqs62x = iqs621_als->iqs62x;
375 unsigned int thresh_reg, thresh_val;
376 u8 ir_flags_mask, *thresh_cache;
377 int ret = -EINVAL;
379 mutex_lock(&iqs621_als->lock);
381 switch (dir) {
382 case IIO_EV_DIR_RISING:
383 thresh_reg = IQS621_ALS_THRESH_LIGHT;
384 thresh_val = val / 16;
386 thresh_cache = &iqs621_als->thresh_light;
387 ir_flags_mask = 0;
388 break;
390 case IIO_EV_DIR_FALLING:
391 thresh_reg = IQS621_ALS_THRESH_DARK;
392 thresh_val = val / 4;
394 thresh_cache = &iqs621_als->thresh_dark;
395 ir_flags_mask = 0;
396 break;
398 case IIO_EV_DIR_EITHER:
400 * The IQS622 supports two detection thresholds, both measured
401 * in the same arbitrary units reported by read_raw: proximity
402 * (0 through 255 in steps of 1), and touch (0 through 1020 in
403 * steps of 4).
405 * Based on the single detection threshold chosen by the user,
406 * select the hardware threshold that gives the best trade-off
407 * between range and resolution.
409 * By default, the close-range (but coarse) touch threshold is
410 * chosen during probe.
412 switch (val) {
413 case 0 ... 255:
414 thresh_reg = IQS622_IR_THRESH_PROX;
415 thresh_val = val;
417 ir_flags_mask = IQS622_IR_FLAGS_PROX;
418 break;
420 case 256 ... 1020:
421 thresh_reg = IQS622_IR_THRESH_TOUCH;
422 thresh_val = val / 4;
424 ir_flags_mask = IQS622_IR_FLAGS_TOUCH;
425 break;
427 default:
428 goto err_mutex;
431 thresh_cache = &iqs621_als->thresh_prox;
432 break;
434 default:
435 goto err_mutex;
438 if (thresh_val > 0xFF)
439 goto err_mutex;
441 ret = regmap_write(iqs62x->regmap, thresh_reg, thresh_val);
442 if (ret)
443 goto err_mutex;
445 *thresh_cache = thresh_val;
446 iqs621_als->ir_flags_mask = ir_flags_mask;
448 err_mutex:
449 mutex_unlock(&iqs621_als->lock);
451 return ret;
454 static const struct iio_info iqs621_als_info = {
455 .read_raw = &iqs621_als_read_raw,
456 .read_event_config = iqs621_als_read_event_config,
457 .write_event_config = iqs621_als_write_event_config,
458 .read_event_value = iqs621_als_read_event_value,
459 .write_event_value = iqs621_als_write_event_value,
462 static const struct iio_event_spec iqs621_als_range_events[] = {
464 .type = IIO_EV_TYPE_CHANGE,
465 .dir = IIO_EV_DIR_EITHER,
466 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
470 static const struct iio_event_spec iqs621_als_light_events[] = {
472 .type = IIO_EV_TYPE_THRESH,
473 .dir = IIO_EV_DIR_EITHER,
474 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
477 .type = IIO_EV_TYPE_THRESH,
478 .dir = IIO_EV_DIR_RISING,
479 .mask_separate = BIT(IIO_EV_INFO_VALUE),
482 .type = IIO_EV_TYPE_THRESH,
483 .dir = IIO_EV_DIR_FALLING,
484 .mask_separate = BIT(IIO_EV_INFO_VALUE),
488 static const struct iio_chan_spec iqs621_als_channels[] = {
490 .type = IIO_INTENSITY,
491 .address = IQS621_ALS_FLAGS,
492 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
493 .event_spec = iqs621_als_range_events,
494 .num_event_specs = ARRAY_SIZE(iqs621_als_range_events),
497 .type = IIO_LIGHT,
498 .address = IQS621_ALS_UI_OUT,
499 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
500 .event_spec = iqs621_als_light_events,
501 .num_event_specs = ARRAY_SIZE(iqs621_als_light_events),
505 static const struct iio_event_spec iqs622_als_prox_events[] = {
507 .type = IIO_EV_TYPE_THRESH,
508 .dir = IIO_EV_DIR_EITHER,
509 .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
510 BIT(IIO_EV_INFO_VALUE),
514 static const struct iio_chan_spec iqs622_als_channels[] = {
516 .type = IIO_INTENSITY,
517 .channel2 = IIO_MOD_LIGHT_BOTH,
518 .address = IQS622_ALS_FLAGS,
519 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
520 .event_spec = iqs621_als_range_events,
521 .num_event_specs = ARRAY_SIZE(iqs621_als_range_events),
522 .modified = true,
525 .type = IIO_INTENSITY,
526 .channel2 = IIO_MOD_LIGHT_IR,
527 .address = IQS622_IR_RANGE,
528 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
529 .modified = true,
532 .type = IIO_PROXIMITY,
533 .address = IQS622_IR_UI_OUT,
534 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
535 .event_spec = iqs622_als_prox_events,
536 .num_event_specs = ARRAY_SIZE(iqs622_als_prox_events),
540 static int iqs621_als_probe(struct platform_device *pdev)
542 struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent);
543 struct iqs621_als_private *iqs621_als;
544 struct iio_dev *indio_dev;
545 unsigned int val;
546 int ret;
548 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*iqs621_als));
549 if (!indio_dev)
550 return -ENOMEM;
552 iqs621_als = iio_priv(indio_dev);
553 iqs621_als->iqs62x = iqs62x;
555 if (iqs62x->dev_desc->prod_num == IQS622_PROD_NUM) {
556 ret = regmap_read(iqs62x->regmap, IQS622_IR_THRESH_TOUCH,
557 &val);
558 if (ret)
559 return ret;
560 iqs621_als->thresh_prox = val;
561 iqs621_als->ir_flags_mask = IQS622_IR_FLAGS_TOUCH;
563 indio_dev->channels = iqs622_als_channels;
564 indio_dev->num_channels = ARRAY_SIZE(iqs622_als_channels);
565 } else {
566 ret = regmap_read(iqs62x->regmap, IQS621_ALS_THRESH_LIGHT,
567 &val);
568 if (ret)
569 return ret;
570 iqs621_als->thresh_light = val;
572 ret = regmap_read(iqs62x->regmap, IQS621_ALS_THRESH_DARK,
573 &val);
574 if (ret)
575 return ret;
576 iqs621_als->thresh_dark = val;
578 indio_dev->channels = iqs621_als_channels;
579 indio_dev->num_channels = ARRAY_SIZE(iqs621_als_channels);
582 indio_dev->modes = INDIO_DIRECT_MODE;
583 indio_dev->dev.parent = &pdev->dev;
584 indio_dev->name = iqs62x->dev_desc->dev_name;
585 indio_dev->info = &iqs621_als_info;
587 mutex_init(&iqs621_als->lock);
589 iqs621_als->notifier.notifier_call = iqs621_als_notifier;
590 ret = blocking_notifier_chain_register(&iqs621_als->iqs62x->nh,
591 &iqs621_als->notifier);
592 if (ret) {
593 dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret);
594 return ret;
597 ret = devm_add_action_or_reset(&pdev->dev,
598 iqs621_als_notifier_unregister,
599 iqs621_als);
600 if (ret)
601 return ret;
603 return devm_iio_device_register(&pdev->dev, indio_dev);
606 static struct platform_driver iqs621_als_platform_driver = {
607 .driver = {
608 .name = "iqs621-als",
610 .probe = iqs621_als_probe,
612 module_platform_driver(iqs621_als_platform_driver);
614 MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
615 MODULE_DESCRIPTION("Azoteq IQS621/622 Ambient Light Sensors");
616 MODULE_LICENSE("GPL");
617 MODULE_ALIAS("platform:iqs621-als");