1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2023 Axis Communications AB
5 * Datasheet: https://www.ti.com/lit/gpn/opt4001
7 * Device driver for the Texas Instruments OPT4001.
10 #include <linux/bitfield.h>
11 #include <linux/i2c.h>
12 #include <linux/iio/iio.h>
13 #include <linux/math64.h>
14 #include <linux/module.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
19 /* OPT4001 register set */
20 #define OPT4001_LIGHT1_MSB 0x00
21 #define OPT4001_LIGHT1_LSB 0x01
22 #define OPT4001_CTRL 0x0A
23 #define OPT4001_DEVICE_ID 0x11
25 /* OPT4001 register mask */
26 #define OPT4001_EXPONENT_MASK GENMASK(15, 12)
27 #define OPT4001_MSB_MASK GENMASK(11, 0)
28 #define OPT4001_LSB_MASK GENMASK(15, 8)
29 #define OPT4001_COUNTER_MASK GENMASK(7, 4)
30 #define OPT4001_CRC_MASK GENMASK(3, 0)
32 /* OPT4001 device id mask */
33 #define OPT4001_DEVICE_ID_MASK GENMASK(11, 0)
35 /* OPT4001 control registers mask */
36 #define OPT4001_CTRL_QWAKE_MASK GENMASK(15, 15)
37 #define OPT4001_CTRL_RANGE_MASK GENMASK(13, 10)
38 #define OPT4001_CTRL_CONV_TIME_MASK GENMASK(9, 6)
39 #define OPT4001_CTRL_OPER_MODE_MASK GENMASK(5, 4)
40 #define OPT4001_CTRL_LATCH_MASK GENMASK(3, 3)
41 #define OPT4001_CTRL_INT_POL_MASK GENMASK(2, 2)
42 #define OPT4001_CTRL_FAULT_COUNT GENMASK(0, 1)
44 /* OPT4001 constants */
45 #define OPT4001_DEVICE_ID_VAL 0x121
47 /* OPT4001 operating modes */
48 #define OPT4001_CTRL_OPER_MODE_OFF 0x0
49 #define OPT4001_CTRL_OPER_MODE_FORCED 0x1
50 #define OPT4001_CTRL_OPER_MODE_ONE_SHOT 0x2
51 #define OPT4001_CTRL_OPER_MODE_CONTINUOUS 0x3
53 /* OPT4001 conversion control register definitions */
54 #define OPT4001_CTRL_CONVERSION_0_6MS 0x0
55 #define OPT4001_CTRL_CONVERSION_1MS 0x1
56 #define OPT4001_CTRL_CONVERSION_1_8MS 0x2
57 #define OPT4001_CTRL_CONVERSION_3_4MS 0x3
58 #define OPT4001_CTRL_CONVERSION_6_5MS 0x4
59 #define OPT4001_CTRL_CONVERSION_12_7MS 0x5
60 #define OPT4001_CTRL_CONVERSION_25MS 0x6
61 #define OPT4001_CTRL_CONVERSION_50MS 0x7
62 #define OPT4001_CTRL_CONVERSION_100MS 0x8
63 #define OPT4001_CTRL_CONVERSION_200MS 0x9
64 #define OPT4001_CTRL_CONVERSION_400MS 0xa
65 #define OPT4001_CTRL_CONVERSION_800MS 0xb
67 /* OPT4001 scale light level range definitions */
68 #define OPT4001_CTRL_LIGHT_SCALE_AUTO 12
70 /* OPT4001 default values */
71 #define OPT4001_DEFAULT_CONVERSION_TIME OPT4001_CTRL_CONVERSION_800MS
74 * The different packaging of OPT4001 has different constants used when calculating
77 struct opt4001_chip_info {
84 struct regmap *regmap;
85 struct i2c_client *client;
87 const struct opt4001_chip_info *chip_info;
90 static const struct opt4001_chip_info opt4001_sot_5x3_info = {
93 .name = "opt4001-sot-5x3"
96 static const struct opt4001_chip_info opt4001_picostar_info = {
99 .name = "opt4001-picostar"
102 static const int opt4001_int_time_available[][2] = {
118 * Conversion time is integration time + time to set register
119 * this is used as integration time.
121 static const int opt4001_int_time_reg[][2] = {
122 { 600, OPT4001_CTRL_CONVERSION_0_6MS },
123 { 1000, OPT4001_CTRL_CONVERSION_1MS },
124 { 1800, OPT4001_CTRL_CONVERSION_1_8MS },
125 { 3400, OPT4001_CTRL_CONVERSION_3_4MS },
126 { 6500, OPT4001_CTRL_CONVERSION_6_5MS },
127 { 12700, OPT4001_CTRL_CONVERSION_12_7MS },
128 { 25000, OPT4001_CTRL_CONVERSION_25MS },
129 { 50000, OPT4001_CTRL_CONVERSION_50MS },
130 { 100000, OPT4001_CTRL_CONVERSION_100MS },
131 { 200000, OPT4001_CTRL_CONVERSION_200MS },
132 { 400000, OPT4001_CTRL_CONVERSION_400MS },
133 { 800000, OPT4001_CTRL_CONVERSION_800MS },
136 static int opt4001_als_time_to_index(const u32 als_integration_time)
140 for (i = 0; i < ARRAY_SIZE(opt4001_int_time_available); i++) {
141 if (als_integration_time == opt4001_int_time_available[i][1])
148 static u8 opt4001_calculate_crc(u8 exp, u32 mantissa, u8 count)
152 crc = (hweight32(mantissa) + hweight32(exp) + hweight32(count)) % 2;
153 crc |= ((hweight32(mantissa & 0xAAAAA) + hweight32(exp & 0xA)
154 + hweight32(count & 0xA)) % 2) << 1;
155 crc |= ((hweight32(mantissa & 0x88888) + hweight32(exp & 0x8)
156 + hweight32(count & 0x8)) % 2) << 2;
157 crc |= (hweight32(mantissa & 0x80808) % 2) << 3;
162 static int opt4001_read_lux_value(struct iio_dev *indio_dev,
165 struct opt4001_chip *chip = iio_priv(indio_dev);
166 struct device *dev = &chip->client->dev;
178 ret = regmap_read(chip->regmap, OPT4001_LIGHT1_MSB, &light1);
180 dev_err(dev, "Failed to read data bytes");
184 ret = regmap_read(chip->regmap, OPT4001_LIGHT1_LSB, &light2);
186 dev_err(dev, "Failed to read data bytes");
190 count = FIELD_GET(OPT4001_COUNTER_MASK, light2);
191 exp = FIELD_GET(OPT4001_EXPONENT_MASK, light1);
192 crc = FIELD_GET(OPT4001_CRC_MASK, light2);
193 msb = FIELD_GET(OPT4001_MSB_MASK, light1);
194 lsb = FIELD_GET(OPT4001_LSB_MASK, light2);
195 lux_raw = (msb << 8) + lsb;
196 calc_crc = opt4001_calculate_crc(exp, lux_raw, count);
200 lux_raw = lux_raw << exp;
201 lux_raw = lux_raw * chip->chip_info->mul;
202 *val = div_u64_rem(lux_raw, chip->chip_info->div, val2);
205 return IIO_VAL_INT_PLUS_NANO;
208 static int opt4001_set_conf(struct opt4001_chip *chip)
210 struct device *dev = &chip->client->dev;
214 reg = FIELD_PREP(OPT4001_CTRL_RANGE_MASK, OPT4001_CTRL_LIGHT_SCALE_AUTO);
215 reg |= FIELD_PREP(OPT4001_CTRL_CONV_TIME_MASK, chip->int_time);
216 reg |= FIELD_PREP(OPT4001_CTRL_OPER_MODE_MASK, OPT4001_CTRL_OPER_MODE_CONTINUOUS);
218 ret = regmap_write(chip->regmap, OPT4001_CTRL, reg);
220 dev_err(dev, "Failed to set configuration\n");
225 static int opt4001_power_down(struct opt4001_chip *chip)
227 struct device *dev = &chip->client->dev;
231 ret = regmap_read(chip->regmap, OPT4001_DEVICE_ID, ®);
233 dev_err(dev, "Failed to read configuration\n");
237 /* MODE_OFF is 0x0 so just set bits to 0 */
238 reg &= ~OPT4001_CTRL_OPER_MODE_MASK;
240 ret = regmap_write(chip->regmap, OPT4001_CTRL, reg);
242 dev_err(dev, "Failed to set configuration to power down\n");
247 static void opt4001_chip_off_action(void *data)
249 struct opt4001_chip *chip = data;
251 opt4001_power_down(chip);
254 static const struct iio_chan_spec opt4001_channels[] = {
257 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
258 .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME),
259 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME)
263 static int opt4001_read_raw(struct iio_dev *indio_dev,
264 struct iio_chan_spec const *chan,
265 int *val, int *val2, long mask)
267 struct opt4001_chip *chip = iio_priv(indio_dev);
270 case IIO_CHAN_INFO_PROCESSED:
271 return opt4001_read_lux_value(indio_dev, val, val2);
272 case IIO_CHAN_INFO_INT_TIME:
274 *val2 = opt4001_int_time_reg[chip->int_time][0];
275 return IIO_VAL_INT_PLUS_MICRO;
281 static int opt4001_write_raw(struct iio_dev *indio_dev,
282 struct iio_chan_spec const *chan,
283 int val, int val2, long mask)
285 struct opt4001_chip *chip = iio_priv(indio_dev);
289 case IIO_CHAN_INFO_INT_TIME:
290 int_time = opt4001_als_time_to_index(val2);
293 chip->int_time = int_time;
294 return opt4001_set_conf(chip);
300 static int opt4001_read_available(struct iio_dev *indio_dev,
301 struct iio_chan_spec const *chan,
302 const int **vals, int *type, int *length,
306 case IIO_CHAN_INFO_INT_TIME:
307 *length = ARRAY_SIZE(opt4001_int_time_available) * 2;
308 *vals = (const int *)opt4001_int_time_available;
309 *type = IIO_VAL_INT_PLUS_MICRO;
310 return IIO_AVAIL_LIST;
317 static const struct iio_info opt4001_info_no_irq = {
318 .read_raw = opt4001_read_raw,
319 .write_raw = opt4001_write_raw,
320 .read_avail = opt4001_read_available,
323 static int opt4001_load_defaults(struct opt4001_chip *chip)
325 chip->int_time = OPT4001_DEFAULT_CONVERSION_TIME;
327 return opt4001_set_conf(chip);
330 static bool opt4001_readable_reg(struct device *dev, unsigned int reg)
333 case OPT4001_LIGHT1_MSB:
334 case OPT4001_LIGHT1_LSB:
336 case OPT4001_DEVICE_ID:
343 static bool opt4001_writable_reg(struct device *dev, unsigned int reg)
353 static bool opt4001_volatile_reg(struct device *dev, unsigned int reg)
356 case OPT4001_LIGHT1_MSB:
357 case OPT4001_LIGHT1_LSB:
364 static const struct regmap_config opt4001_regmap_config = {
368 .cache_type = REGCACHE_RBTREE,
369 .max_register = OPT4001_DEVICE_ID,
370 .readable_reg = opt4001_readable_reg,
371 .writeable_reg = opt4001_writable_reg,
372 .volatile_reg = opt4001_volatile_reg,
373 .val_format_endian = REGMAP_ENDIAN_BIG,
376 static int opt4001_probe(struct i2c_client *client)
378 struct opt4001_chip *chip;
379 struct iio_dev *indio_dev;
383 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
387 chip = iio_priv(indio_dev);
389 ret = devm_regulator_get_enable(&client->dev, "vdd");
391 return dev_err_probe(&client->dev, ret, "Failed to enable vdd supply\n");
393 chip->regmap = devm_regmap_init_i2c(client, &opt4001_regmap_config);
394 if (IS_ERR(chip->regmap))
395 return dev_err_probe(&client->dev, PTR_ERR(chip->regmap),
396 "regmap initialization failed\n");
397 chip->client = client;
399 indio_dev->info = &opt4001_info_no_irq;
401 ret = regmap_reinit_cache(chip->regmap, &opt4001_regmap_config);
403 return dev_err_probe(&client->dev, ret,
404 "failed to reinit regmap cache\n");
406 ret = regmap_read(chip->regmap, OPT4001_DEVICE_ID, &dev_id);
408 return dev_err_probe(&client->dev, ret,
409 "Failed to read the device ID register\n");
411 dev_id = FIELD_GET(OPT4001_DEVICE_ID_MASK, dev_id);
412 if (dev_id != OPT4001_DEVICE_ID_VAL)
413 dev_warn(&client->dev, "Device ID: %#04x unknown\n", dev_id);
415 chip->chip_info = i2c_get_match_data(client);
417 indio_dev->channels = opt4001_channels;
418 indio_dev->num_channels = ARRAY_SIZE(opt4001_channels);
419 indio_dev->modes = INDIO_DIRECT_MODE;
420 indio_dev->name = chip->chip_info->name;
422 ret = opt4001_load_defaults(chip);
424 return dev_err_probe(&client->dev, ret,
425 "Failed to set sensor defaults\n");
427 ret = devm_add_action_or_reset(&client->dev,
428 opt4001_chip_off_action,
431 return dev_err_probe(&client->dev, ret,
432 "Failed to setup power off action\n");
434 return devm_iio_device_register(&client->dev, indio_dev);
438 * The compatible string determines which constants to use depending on
441 static const struct i2c_device_id opt4001_id[] = {
442 { "opt4001-sot-5x3", (kernel_ulong_t)&opt4001_sot_5x3_info },
443 { "opt4001-picostar", (kernel_ulong_t)&opt4001_picostar_info },
446 MODULE_DEVICE_TABLE(i2c, opt4001_id);
448 static const struct of_device_id opt4001_of_match[] = {
449 { .compatible = "ti,opt4001-sot-5x3", .data = &opt4001_sot_5x3_info},
450 { .compatible = "ti,opt4001-picostar", .data = &opt4001_picostar_info},
453 MODULE_DEVICE_TABLE(of, opt4001_of_match);
455 static struct i2c_driver opt4001_driver = {
458 .of_match_table = opt4001_of_match,
460 .probe = opt4001_probe,
461 .id_table = opt4001_id,
463 module_i2c_driver(opt4001_driver);
466 MODULE_DESCRIPTION("Texas Instruments opt4001 ambient light sensor driver");
467 MODULE_LICENSE("GPL");