isl76682.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IIO driver for the light sensor ISL76682.
  4. * ISL76682 is Ambient Light Sensor
  5. *
  6. * Copyright (c) 2023 Marek Vasut <marex@denx.de>
  7. */
  8. #include <linux/array_size.h>
  9. #include <linux/bits.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/regmap.h>
  17. #include <linux/types.h>
  18. #include <linux/iio/iio.h>
  19. #define ISL76682_REG_COMMAND 0x00
  20. #define ISL76682_COMMAND_EN BIT(7)
  21. #define ISL76682_COMMAND_MODE_CONTINUOUS BIT(6)
  22. #define ISL76682_COMMAND_LIGHT_IR BIT(5)
  23. #define ISL76682_COMMAND_RANGE_LUX_1K 0x0
  24. #define ISL76682_COMMAND_RANGE_LUX_4K 0x1
  25. #define ISL76682_COMMAND_RANGE_LUX_16K 0x2
  26. #define ISL76682_COMMAND_RANGE_LUX_64K 0x3
  27. #define ISL76682_COMMAND_RANGE_LUX_MASK GENMASK(1, 0)
  28. #define ISL76682_REG_ALSIR_L 0x01
  29. #define ISL76682_REG_ALSIR_U 0x02
  30. #define ISL76682_NUM_REGS (ISL76682_REG_ALSIR_U + 1)
  31. #define ISL76682_CONV_TIME_MS 100
  32. #define ISL76682_INT_TIME_US 90000
  33. #define ISL76682_ADC_MAX (BIT(16) - 1)
  34. struct isl76682_chip {
  35. /*
  36. * Lock to synchronize access to device command register
  37. * and the content of range variable below.
  38. */
  39. struct mutex lock;
  40. struct regmap *regmap;
  41. u8 range;
  42. u8 command;
  43. };
  44. struct isl76682_range {
  45. u8 range;
  46. u32 als;
  47. u32 ir;
  48. };
  49. static struct isl76682_range isl76682_range_table[] = {
  50. { ISL76682_COMMAND_RANGE_LUX_1K, 15000, 10500 },
  51. { ISL76682_COMMAND_RANGE_LUX_4K, 60000, 42000 },
  52. { ISL76682_COMMAND_RANGE_LUX_16K, 240000, 168000 },
  53. { ISL76682_COMMAND_RANGE_LUX_64K, 960000, 673000 }
  54. };
  55. static int isl76682_get(struct isl76682_chip *chip, bool mode_ir, int *data)
  56. {
  57. u8 command;
  58. int ret;
  59. command = ISL76682_COMMAND_EN | ISL76682_COMMAND_MODE_CONTINUOUS |
  60. chip->range;
  61. if (mode_ir)
  62. command |= ISL76682_COMMAND_LIGHT_IR;
  63. if (command != chip->command) {
  64. ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, command);
  65. if (ret)
  66. return ret;
  67. /* Need to wait for conversion time if ALS/IR mode enabled */
  68. msleep(ISL76682_CONV_TIME_MS);
  69. chip->command = command;
  70. }
  71. ret = regmap_bulk_read(chip->regmap, ISL76682_REG_ALSIR_L, data, 2);
  72. *data &= ISL76682_ADC_MAX;
  73. return ret;
  74. }
  75. static int isl76682_write_raw(struct iio_dev *indio_dev,
  76. struct iio_chan_spec const *chan,
  77. int val, int val2, long mask)
  78. {
  79. struct isl76682_chip *chip = iio_priv(indio_dev);
  80. int i;
  81. if (mask != IIO_CHAN_INFO_SCALE)
  82. return -EINVAL;
  83. if (val != 0)
  84. return -EINVAL;
  85. for (i = 0; i < ARRAY_SIZE(isl76682_range_table); i++) {
  86. if (chan->type == IIO_LIGHT && val2 != isl76682_range_table[i].als)
  87. continue;
  88. if (chan->type == IIO_INTENSITY && val2 != isl76682_range_table[i].ir)
  89. continue;
  90. scoped_guard(mutex, &chip->lock)
  91. chip->range = isl76682_range_table[i].range;
  92. return 0;
  93. }
  94. return -EINVAL;
  95. }
  96. static int isl76682_read_raw(struct iio_dev *indio_dev,
  97. struct iio_chan_spec const *chan,
  98. int *val, int *val2, long mask)
  99. {
  100. struct isl76682_chip *chip = iio_priv(indio_dev);
  101. int ret;
  102. int i;
  103. guard(mutex)(&chip->lock);
  104. switch (mask) {
  105. case IIO_CHAN_INFO_RAW:
  106. switch (chan->type) {
  107. case IIO_LIGHT:
  108. ret = isl76682_get(chip, false, val);
  109. return (ret < 0) ? ret : IIO_VAL_INT;
  110. case IIO_INTENSITY:
  111. ret = isl76682_get(chip, true, val);
  112. return (ret < 0) ? ret : IIO_VAL_INT;
  113. default:
  114. return -EINVAL;
  115. }
  116. case IIO_CHAN_INFO_SCALE:
  117. for (i = 0; i < ARRAY_SIZE(isl76682_range_table); i++) {
  118. if (chip->range != isl76682_range_table[i].range)
  119. continue;
  120. *val = 0;
  121. switch (chan->type) {
  122. case IIO_LIGHT:
  123. *val2 = isl76682_range_table[i].als;
  124. return IIO_VAL_INT_PLUS_MICRO;
  125. case IIO_INTENSITY:
  126. *val2 = isl76682_range_table[i].ir;
  127. return IIO_VAL_INT_PLUS_MICRO;
  128. default:
  129. return -EINVAL;
  130. }
  131. }
  132. return -EINVAL;
  133. case IIO_CHAN_INFO_INT_TIME:
  134. *val = 0;
  135. *val2 = ISL76682_INT_TIME_US;
  136. return IIO_VAL_INT_PLUS_MICRO;
  137. default:
  138. return -EINVAL;
  139. }
  140. }
  141. static int illuminance_scale_available[] = {
  142. 0, 15000,
  143. 0, 60000,
  144. 0, 240000,
  145. 0, 960000,
  146. };
  147. static int intensity_scale_available[] = {
  148. 0, 10500,
  149. 0, 42000,
  150. 0, 168000,
  151. 0, 673000,
  152. };
  153. static int isl76682_read_avail(struct iio_dev *indio_dev,
  154. struct iio_chan_spec const *chan,
  155. const int **vals, int *type,
  156. int *length, long mask)
  157. {
  158. switch (mask) {
  159. case IIO_CHAN_INFO_SCALE:
  160. switch (chan->type) {
  161. case IIO_LIGHT:
  162. *vals = illuminance_scale_available;
  163. *length = ARRAY_SIZE(illuminance_scale_available);
  164. *type = IIO_VAL_INT_PLUS_MICRO;
  165. return IIO_AVAIL_LIST;
  166. case IIO_INTENSITY:
  167. *vals = intensity_scale_available;
  168. *length = ARRAY_SIZE(intensity_scale_available);
  169. *type = IIO_VAL_INT_PLUS_MICRO;
  170. return IIO_AVAIL_LIST;
  171. default:
  172. return -EINVAL;
  173. }
  174. default:
  175. return -EINVAL;
  176. }
  177. }
  178. static const struct iio_chan_spec isl76682_channels[] = {
  179. {
  180. .type = IIO_LIGHT,
  181. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  182. BIT(IIO_CHAN_INFO_SCALE),
  183. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE),
  184. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
  185. }, {
  186. .type = IIO_INTENSITY,
  187. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  188. BIT(IIO_CHAN_INFO_SCALE),
  189. .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE),
  190. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
  191. }
  192. };
  193. static const struct iio_info isl76682_info = {
  194. .read_avail = isl76682_read_avail,
  195. .read_raw = isl76682_read_raw,
  196. .write_raw = isl76682_write_raw,
  197. };
  198. static int isl76682_clear_configure_reg(struct isl76682_chip *chip)
  199. {
  200. struct device *dev = regmap_get_device(chip->regmap);
  201. int ret;
  202. ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, 0x0);
  203. if (ret < 0)
  204. dev_err(dev, "Error %d clearing the CONFIGURE register\n", ret);
  205. /*
  206. * In the success case, the command register was zeroed out.
  207. *
  208. * In the error case, we do not know in which state the command
  209. * register is, so we assume it is zeroed out, so that it would
  210. * be reprogrammed at the next data read out, and at that time
  211. * we hope it would be reprogrammed successfully. That is very
  212. * much a best effort approach.
  213. */
  214. chip->command = 0;
  215. return ret;
  216. }
  217. static void isl76682_reset_action(void *chip)
  218. {
  219. isl76682_clear_configure_reg(chip);
  220. }
  221. static bool isl76682_is_volatile_reg(struct device *dev, unsigned int reg)
  222. {
  223. switch (reg) {
  224. case ISL76682_REG_ALSIR_L:
  225. case ISL76682_REG_ALSIR_U:
  226. return true;
  227. default:
  228. return false;
  229. }
  230. }
  231. static const struct regmap_config isl76682_regmap_config = {
  232. .reg_bits = 8,
  233. .val_bits = 8,
  234. .volatile_reg = isl76682_is_volatile_reg,
  235. .max_register = ISL76682_NUM_REGS - 1,
  236. .num_reg_defaults_raw = ISL76682_NUM_REGS,
  237. .cache_type = REGCACHE_FLAT,
  238. };
  239. static int isl76682_probe(struct i2c_client *client)
  240. {
  241. struct device *dev = &client->dev;
  242. struct isl76682_chip *chip;
  243. struct iio_dev *indio_dev;
  244. int ret;
  245. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  246. if (!indio_dev)
  247. return -ENOMEM;
  248. chip = iio_priv(indio_dev);
  249. mutex_init(&chip->lock);
  250. chip->regmap = devm_regmap_init_i2c(client, &isl76682_regmap_config);
  251. ret = PTR_ERR_OR_ZERO(chip->regmap);
  252. if (ret)
  253. return dev_err_probe(dev, ret, "Error initializing regmap\n");
  254. chip->range = ISL76682_COMMAND_RANGE_LUX_1K;
  255. ret = isl76682_clear_configure_reg(chip);
  256. if (ret < 0)
  257. return ret;
  258. ret = devm_add_action_or_reset(dev, isl76682_reset_action, chip);
  259. if (ret)
  260. return ret;
  261. indio_dev->info = &isl76682_info;
  262. indio_dev->channels = isl76682_channels;
  263. indio_dev->num_channels = ARRAY_SIZE(isl76682_channels);
  264. indio_dev->name = "isl76682";
  265. indio_dev->modes = INDIO_DIRECT_MODE;
  266. return devm_iio_device_register(dev, indio_dev);
  267. }
  268. static const struct i2c_device_id isl76682_id[] = {
  269. { "isl76682" },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(i2c, isl76682_id);
  273. static const struct of_device_id isl76682_of_match[] = {
  274. { .compatible = "isil,isl76682" },
  275. { }
  276. };
  277. MODULE_DEVICE_TABLE(of, isl76682_of_match);
  278. static struct i2c_driver isl76682_driver = {
  279. .driver = {
  280. .name = "isl76682",
  281. .of_match_table = isl76682_of_match,
  282. },
  283. .probe = isl76682_probe,
  284. .id_table = isl76682_id,
  285. };
  286. module_i2c_driver(isl76682_driver);
  287. MODULE_DESCRIPTION("ISL76682 Ambient Light Sensor driver");
  288. MODULE_LICENSE("GPL");
  289. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");