dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / iio / light / max44000.c
blobd3fb460cfbdce575f3bb0f2be5d1057130567637
1 /*
2 * MAX44000 Ambient and Infrared Proximity Sensor
4 * Copyright (c) 2016, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * Data sheet: https://datasheets.maximintegrated.com/en/ds/MAX44000.pdf
12 * 7-bit I2C slave address 0x4a
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/util_macros.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/triggered_buffer.h>
25 #include <linux/acpi.h>
27 #define MAX44000_DRV_NAME "max44000"
29 /* Registers in datasheet order */
30 #define MAX44000_REG_STATUS 0x00
31 #define MAX44000_REG_CFG_MAIN 0x01
32 #define MAX44000_REG_CFG_RX 0x02
33 #define MAX44000_REG_CFG_TX 0x03
34 #define MAX44000_REG_ALS_DATA_HI 0x04
35 #define MAX44000_REG_ALS_DATA_LO 0x05
36 #define MAX44000_REG_PRX_DATA 0x16
37 #define MAX44000_REG_ALS_UPTHR_HI 0x06
38 #define MAX44000_REG_ALS_UPTHR_LO 0x07
39 #define MAX44000_REG_ALS_LOTHR_HI 0x08
40 #define MAX44000_REG_ALS_LOTHR_LO 0x09
41 #define MAX44000_REG_PST 0x0a
42 #define MAX44000_REG_PRX_IND 0x0b
43 #define MAX44000_REG_PRX_THR 0x0c
44 #define MAX44000_REG_TRIM_GAIN_GREEN 0x0f
45 #define MAX44000_REG_TRIM_GAIN_IR 0x10
47 /* REG_CFG bits */
48 #define MAX44000_CFG_ALSINTE 0x01
49 #define MAX44000_CFG_PRXINTE 0x02
50 #define MAX44000_CFG_MASK 0x1c
51 #define MAX44000_CFG_MODE_SHUTDOWN 0x00
52 #define MAX44000_CFG_MODE_ALS_GIR 0x04
53 #define MAX44000_CFG_MODE_ALS_G 0x08
54 #define MAX44000_CFG_MODE_ALS_IR 0x0c
55 #define MAX44000_CFG_MODE_ALS_PRX 0x10
56 #define MAX44000_CFG_MODE_PRX 0x14
57 #define MAX44000_CFG_TRIM 0x20
60 * Upper 4 bits are not documented but start as 1 on powerup
61 * Setting them to 0 causes proximity to misbehave so set them to 1
63 #define MAX44000_REG_CFG_RX_DEFAULT 0xf0
65 /* REG_RX bits */
66 #define MAX44000_CFG_RX_ALSTIM_MASK 0x0c
67 #define MAX44000_CFG_RX_ALSTIM_SHIFT 2
68 #define MAX44000_CFG_RX_ALSPGA_MASK 0x03
69 #define MAX44000_CFG_RX_ALSPGA_SHIFT 0
71 /* REG_TX bits */
72 #define MAX44000_LED_CURRENT_MASK 0xf
73 #define MAX44000_LED_CURRENT_MAX 11
74 #define MAX44000_LED_CURRENT_DEFAULT 6
76 #define MAX44000_ALSDATA_OVERFLOW 0x4000
78 struct max44000_data {
79 struct mutex lock;
80 struct regmap *regmap;
83 /* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */
84 #define MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 5
86 /* Scale can be multiplied by up to 128x via ALSPGA for measurement gain */
87 static const int max44000_alspga_shift[] = {0, 2, 4, 7};
88 #define MAX44000_ALSPGA_MAX_SHIFT 7
91 * Scale can be multiplied by up to 64x via ALSTIM because of lost resolution
93 * This scaling factor is hidden from userspace and instead accounted for when
94 * reading raw values from the device.
96 * This makes it possible to cleanly expose ALSPGA as IIO_CHAN_INFO_SCALE and
97 * ALSTIM as IIO_CHAN_INFO_INT_TIME without the values affecting each other.
99 * Handling this internally is also required for buffer support because the
100 * channel's scan_type can't be modified dynamically.
102 #define MAX44000_ALSTIM_SHIFT(alstim) (2 * (alstim))
104 /* Available integration times with pretty manual alignment: */
105 static const int max44000_int_time_avail_ns_array[] = {
106 100000000,
107 25000000,
108 6250000,
109 1562500,
111 static const char max44000_int_time_avail_str[] =
112 "0.100 "
113 "0.025 "
114 "0.00625 "
115 "0.0015625";
117 /* Available scales (internal to ulux) with pretty manual alignment: */
118 static const int max44000_scale_avail_ulux_array[] = {
119 31250,
120 125000,
121 500000,
122 4000000,
124 static const char max44000_scale_avail_str[] =
125 "0.03125 "
126 "0.125 "
127 "0.5 "
128 "4";
130 #define MAX44000_SCAN_INDEX_ALS 0
131 #define MAX44000_SCAN_INDEX_PRX 1
133 static const struct iio_chan_spec max44000_channels[] = {
135 .type = IIO_LIGHT,
136 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
137 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
138 BIT(IIO_CHAN_INFO_INT_TIME),
139 .scan_index = MAX44000_SCAN_INDEX_ALS,
140 .scan_type = {
141 .sign = 'u',
142 .realbits = 14,
143 .storagebits = 16,
147 .type = IIO_PROXIMITY,
148 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
149 .scan_index = MAX44000_SCAN_INDEX_PRX,
150 .scan_type = {
151 .sign = 'u',
152 .realbits = 8,
153 .storagebits = 16,
156 IIO_CHAN_SOFT_TIMESTAMP(2),
158 .type = IIO_CURRENT,
159 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
160 BIT(IIO_CHAN_INFO_SCALE),
161 .extend_name = "led",
162 .output = 1,
163 .scan_index = -1,
167 static int max44000_read_alstim(struct max44000_data *data)
169 unsigned int val;
170 int ret;
172 ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
173 if (ret < 0)
174 return ret;
175 return (val & MAX44000_CFG_RX_ALSTIM_MASK) >> MAX44000_CFG_RX_ALSTIM_SHIFT;
178 static int max44000_write_alstim(struct max44000_data *data, int val)
180 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
181 MAX44000_CFG_RX_ALSTIM_MASK,
182 val << MAX44000_CFG_RX_ALSTIM_SHIFT);
185 static int max44000_read_alspga(struct max44000_data *data)
187 unsigned int val;
188 int ret;
190 ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val);
191 if (ret < 0)
192 return ret;
193 return (val & MAX44000_CFG_RX_ALSPGA_MASK) >> MAX44000_CFG_RX_ALSPGA_SHIFT;
196 static int max44000_write_alspga(struct max44000_data *data, int val)
198 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX,
199 MAX44000_CFG_RX_ALSPGA_MASK,
200 val << MAX44000_CFG_RX_ALSPGA_SHIFT);
203 static int max44000_read_alsval(struct max44000_data *data)
205 u16 regval;
206 __be16 val;
207 int alstim, ret;
209 ret = regmap_bulk_read(data->regmap, MAX44000_REG_ALS_DATA_HI,
210 &val, sizeof(val));
211 if (ret < 0)
212 return ret;
213 alstim = ret = max44000_read_alstim(data);
214 if (ret < 0)
215 return ret;
217 regval = be16_to_cpu(val);
220 * Overflow is explained on datasheet page 17.
222 * It's a warning that either the G or IR channel has become saturated
223 * and that the value in the register is likely incorrect.
225 * The recommendation is to change the scale (ALSPGA).
226 * The driver just returns the max representable value.
228 if (regval & MAX44000_ALSDATA_OVERFLOW)
229 return 0x3FFF;
231 return regval << MAX44000_ALSTIM_SHIFT(alstim);
234 static int max44000_write_led_current_raw(struct max44000_data *data, int val)
236 /* Maybe we should clamp the value instead? */
237 if (val < 0 || val > MAX44000_LED_CURRENT_MAX)
238 return -ERANGE;
239 if (val >= 8)
240 val += 4;
241 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_TX,
242 MAX44000_LED_CURRENT_MASK, val);
245 static int max44000_read_led_current_raw(struct max44000_data *data)
247 unsigned int regval;
248 int ret;
250 ret = regmap_read(data->regmap, MAX44000_REG_CFG_TX, &regval);
251 if (ret < 0)
252 return ret;
253 regval &= MAX44000_LED_CURRENT_MASK;
254 if (regval >= 8)
255 regval -= 4;
256 return regval;
259 static int max44000_read_raw(struct iio_dev *indio_dev,
260 struct iio_chan_spec const *chan,
261 int *val, int *val2, long mask)
263 struct max44000_data *data = iio_priv(indio_dev);
264 int alstim, alspga;
265 unsigned int regval;
266 int ret;
268 switch (mask) {
269 case IIO_CHAN_INFO_RAW:
270 switch (chan->type) {
271 case IIO_LIGHT:
272 mutex_lock(&data->lock);
273 ret = max44000_read_alsval(data);
274 mutex_unlock(&data->lock);
275 if (ret < 0)
276 return ret;
277 *val = ret;
278 return IIO_VAL_INT;
280 case IIO_PROXIMITY:
281 mutex_lock(&data->lock);
282 ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, &regval);
283 mutex_unlock(&data->lock);
284 if (ret < 0)
285 return ret;
286 *val = regval;
287 return IIO_VAL_INT;
289 case IIO_CURRENT:
290 mutex_lock(&data->lock);
291 ret = max44000_read_led_current_raw(data);
292 mutex_unlock(&data->lock);
293 if (ret < 0)
294 return ret;
295 *val = ret;
296 return IIO_VAL_INT;
298 default:
299 return -EINVAL;
302 case IIO_CHAN_INFO_SCALE:
303 switch (chan->type) {
304 case IIO_CURRENT:
305 /* Output register is in 10s of miliamps */
306 *val = 10;
307 return IIO_VAL_INT;
309 case IIO_LIGHT:
310 mutex_lock(&data->lock);
311 alspga = ret = max44000_read_alspga(data);
312 mutex_unlock(&data->lock);
313 if (ret < 0)
314 return ret;
316 /* Avoid negative shifts */
317 *val = (1 << MAX44000_ALSPGA_MAX_SHIFT);
318 *val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2
319 + MAX44000_ALSPGA_MAX_SHIFT
320 - max44000_alspga_shift[alspga];
321 return IIO_VAL_FRACTIONAL_LOG2;
323 default:
324 return -EINVAL;
327 case IIO_CHAN_INFO_INT_TIME:
328 mutex_lock(&data->lock);
329 alstim = ret = max44000_read_alstim(data);
330 mutex_unlock(&data->lock);
332 if (ret < 0)
333 return ret;
334 *val = 0;
335 *val2 = max44000_int_time_avail_ns_array[alstim];
336 return IIO_VAL_INT_PLUS_NANO;
338 default:
339 return -EINVAL;
343 static int max44000_write_raw(struct iio_dev *indio_dev,
344 struct iio_chan_spec const *chan,
345 int val, int val2, long mask)
347 struct max44000_data *data = iio_priv(indio_dev);
348 int ret;
350 if (mask == IIO_CHAN_INFO_RAW && chan->type == IIO_CURRENT) {
351 mutex_lock(&data->lock);
352 ret = max44000_write_led_current_raw(data, val);
353 mutex_unlock(&data->lock);
354 return ret;
355 } else if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) {
356 s64 valns = val * NSEC_PER_SEC + val2;
357 int alstim = find_closest_descending(valns,
358 max44000_int_time_avail_ns_array,
359 ARRAY_SIZE(max44000_int_time_avail_ns_array));
360 mutex_lock(&data->lock);
361 ret = max44000_write_alstim(data, alstim);
362 mutex_unlock(&data->lock);
363 return ret;
364 } else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) {
365 s64 valus = val * USEC_PER_SEC + val2;
366 int alspga = find_closest(valus,
367 max44000_scale_avail_ulux_array,
368 ARRAY_SIZE(max44000_scale_avail_ulux_array));
369 mutex_lock(&data->lock);
370 ret = max44000_write_alspga(data, alspga);
371 mutex_unlock(&data->lock);
372 return ret;
375 return -EINVAL;
378 static int max44000_write_raw_get_fmt(struct iio_dev *indio_dev,
379 struct iio_chan_spec const *chan,
380 long mask)
382 if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT)
383 return IIO_VAL_INT_PLUS_NANO;
384 else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT)
385 return IIO_VAL_INT_PLUS_MICRO;
386 else
387 return IIO_VAL_INT;
390 static IIO_CONST_ATTR(illuminance_integration_time_available, max44000_int_time_avail_str);
391 static IIO_CONST_ATTR(illuminance_scale_available, max44000_scale_avail_str);
393 static struct attribute *max44000_attributes[] = {
394 &iio_const_attr_illuminance_integration_time_available.dev_attr.attr,
395 &iio_const_attr_illuminance_scale_available.dev_attr.attr,
396 NULL
399 static const struct attribute_group max44000_attribute_group = {
400 .attrs = max44000_attributes,
403 static const struct iio_info max44000_info = {
404 .read_raw = max44000_read_raw,
405 .write_raw = max44000_write_raw,
406 .write_raw_get_fmt = max44000_write_raw_get_fmt,
407 .attrs = &max44000_attribute_group,
410 static bool max44000_readable_reg(struct device *dev, unsigned int reg)
412 switch (reg) {
413 case MAX44000_REG_STATUS:
414 case MAX44000_REG_CFG_MAIN:
415 case MAX44000_REG_CFG_RX:
416 case MAX44000_REG_CFG_TX:
417 case MAX44000_REG_ALS_DATA_HI:
418 case MAX44000_REG_ALS_DATA_LO:
419 case MAX44000_REG_PRX_DATA:
420 case MAX44000_REG_ALS_UPTHR_HI:
421 case MAX44000_REG_ALS_UPTHR_LO:
422 case MAX44000_REG_ALS_LOTHR_HI:
423 case MAX44000_REG_ALS_LOTHR_LO:
424 case MAX44000_REG_PST:
425 case MAX44000_REG_PRX_IND:
426 case MAX44000_REG_PRX_THR:
427 case MAX44000_REG_TRIM_GAIN_GREEN:
428 case MAX44000_REG_TRIM_GAIN_IR:
429 return true;
430 default:
431 return false;
435 static bool max44000_writeable_reg(struct device *dev, unsigned int reg)
437 switch (reg) {
438 case MAX44000_REG_CFG_MAIN:
439 case MAX44000_REG_CFG_RX:
440 case MAX44000_REG_CFG_TX:
441 case MAX44000_REG_ALS_UPTHR_HI:
442 case MAX44000_REG_ALS_UPTHR_LO:
443 case MAX44000_REG_ALS_LOTHR_HI:
444 case MAX44000_REG_ALS_LOTHR_LO:
445 case MAX44000_REG_PST:
446 case MAX44000_REG_PRX_IND:
447 case MAX44000_REG_PRX_THR:
448 case MAX44000_REG_TRIM_GAIN_GREEN:
449 case MAX44000_REG_TRIM_GAIN_IR:
450 return true;
451 default:
452 return false;
456 static bool max44000_volatile_reg(struct device *dev, unsigned int reg)
458 switch (reg) {
459 case MAX44000_REG_STATUS:
460 case MAX44000_REG_ALS_DATA_HI:
461 case MAX44000_REG_ALS_DATA_LO:
462 case MAX44000_REG_PRX_DATA:
463 return true;
464 default:
465 return false;
469 static bool max44000_precious_reg(struct device *dev, unsigned int reg)
471 return reg == MAX44000_REG_STATUS;
474 static const struct regmap_config max44000_regmap_config = {
475 .reg_bits = 8,
476 .val_bits = 8,
478 .max_register = MAX44000_REG_PRX_DATA,
479 .readable_reg = max44000_readable_reg,
480 .writeable_reg = max44000_writeable_reg,
481 .volatile_reg = max44000_volatile_reg,
482 .precious_reg = max44000_precious_reg,
484 .use_single_read = true,
485 .use_single_write = true,
486 .cache_type = REGCACHE_RBTREE,
489 static irqreturn_t max44000_trigger_handler(int irq, void *p)
491 struct iio_poll_func *pf = p;
492 struct iio_dev *indio_dev = pf->indio_dev;
493 struct max44000_data *data = iio_priv(indio_dev);
494 u16 buf[8]; /* 2x u16 + padding + 8 bytes timestamp */
495 int index = 0;
496 unsigned int regval;
497 int ret;
499 mutex_lock(&data->lock);
500 if (test_bit(MAX44000_SCAN_INDEX_ALS, indio_dev->active_scan_mask)) {
501 ret = max44000_read_alsval(data);
502 if (ret < 0)
503 goto out_unlock;
504 buf[index++] = ret;
506 if (test_bit(MAX44000_SCAN_INDEX_PRX, indio_dev->active_scan_mask)) {
507 ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, &regval);
508 if (ret < 0)
509 goto out_unlock;
510 buf[index] = regval;
512 mutex_unlock(&data->lock);
514 iio_push_to_buffers_with_timestamp(indio_dev, buf,
515 iio_get_time_ns(indio_dev));
516 iio_trigger_notify_done(indio_dev->trig);
517 return IRQ_HANDLED;
519 out_unlock:
520 mutex_unlock(&data->lock);
521 iio_trigger_notify_done(indio_dev->trig);
522 return IRQ_HANDLED;
525 static int max44000_probe(struct i2c_client *client,
526 const struct i2c_device_id *id)
528 struct max44000_data *data;
529 struct iio_dev *indio_dev;
530 int ret, reg;
532 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
533 if (!indio_dev)
534 return -ENOMEM;
535 data = iio_priv(indio_dev);
536 data->regmap = devm_regmap_init_i2c(client, &max44000_regmap_config);
537 if (IS_ERR(data->regmap)) {
538 dev_err(&client->dev, "regmap_init failed!\n");
539 return PTR_ERR(data->regmap);
542 i2c_set_clientdata(client, indio_dev);
543 mutex_init(&data->lock);
544 indio_dev->dev.parent = &client->dev;
545 indio_dev->info = &max44000_info;
546 indio_dev->name = MAX44000_DRV_NAME;
547 indio_dev->channels = max44000_channels;
548 indio_dev->num_channels = ARRAY_SIZE(max44000_channels);
551 * The device doesn't have a reset function so we just clear some
552 * important bits at probe time to ensure sane operation.
554 * Since we don't support interrupts/events the threshold values are
555 * not important. We also don't touch trim values.
558 /* Reset ALS scaling bits */
559 ret = regmap_write(data->regmap, MAX44000_REG_CFG_RX,
560 MAX44000_REG_CFG_RX_DEFAULT);
561 if (ret < 0) {
562 dev_err(&client->dev, "failed to write default CFG_RX: %d\n",
563 ret);
564 return ret;
568 * By default the LED pulse used for the proximity sensor is disabled.
569 * Set a middle value so that we get some sort of valid data by default.
571 ret = max44000_write_led_current_raw(data, MAX44000_LED_CURRENT_DEFAULT);
572 if (ret < 0) {
573 dev_err(&client->dev, "failed to write init config: %d\n", ret);
574 return ret;
577 /* Reset CFG bits to ALS_PRX mode which allows easy reading of both values. */
578 reg = MAX44000_CFG_TRIM | MAX44000_CFG_MODE_ALS_PRX;
579 ret = regmap_write(data->regmap, MAX44000_REG_CFG_MAIN, reg);
580 if (ret < 0) {
581 dev_err(&client->dev, "failed to write init config: %d\n", ret);
582 return ret;
585 /* Read status at least once to clear any stale interrupt bits. */
586 ret = regmap_read(data->regmap, MAX44000_REG_STATUS, &reg);
587 if (ret < 0) {
588 dev_err(&client->dev, "failed to read init status: %d\n", ret);
589 return ret;
592 ret = iio_triggered_buffer_setup(indio_dev, NULL, max44000_trigger_handler, NULL);
593 if (ret < 0) {
594 dev_err(&client->dev, "iio triggered buffer setup failed\n");
595 return ret;
598 return iio_device_register(indio_dev);
601 static int max44000_remove(struct i2c_client *client)
603 struct iio_dev *indio_dev = i2c_get_clientdata(client);
605 iio_device_unregister(indio_dev);
606 iio_triggered_buffer_cleanup(indio_dev);
608 return 0;
611 static const struct i2c_device_id max44000_id[] = {
612 {"max44000", 0},
615 MODULE_DEVICE_TABLE(i2c, max44000_id);
617 #ifdef CONFIG_ACPI
618 static const struct acpi_device_id max44000_acpi_match[] = {
619 {"MAX44000", 0},
622 MODULE_DEVICE_TABLE(acpi, max44000_acpi_match);
623 #endif
625 static struct i2c_driver max44000_driver = {
626 .driver = {
627 .name = MAX44000_DRV_NAME,
628 .acpi_match_table = ACPI_PTR(max44000_acpi_match),
630 .probe = max44000_probe,
631 .remove = max44000_remove,
632 .id_table = max44000_id,
635 module_i2c_driver(max44000_driver);
637 MODULE_AUTHOR("Crestez Dan Leonard <leonard.crestez@intel.com>");
638 MODULE_DESCRIPTION("MAX44000 Ambient and Infrared Proximity Sensor");
639 MODULE_LICENSE("GPL v2");