isl29125.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * isl29125.c - Support for Intersil ISL29125 RGB light sensor
  4. *
  5. * Copyright (c) 2014 Peter Meerwald <pmeerw@pmeerw.net>
  6. *
  7. * RGB light sensor with 16-bit channels for red, green, blue);
  8. * 7-bit I2C slave address 0x44
  9. *
  10. * TODO: interrupt support, IR compensation, thresholds, 12bit
  11. */
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/delay.h>
  15. #include <linux/pm.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/trigger_consumer.h>
  19. #include <linux/iio/buffer.h>
  20. #include <linux/iio/triggered_buffer.h>
  21. #define ISL29125_DRV_NAME "isl29125"
  22. #define ISL29125_DEVICE_ID 0x00
  23. #define ISL29125_CONF1 0x01
  24. #define ISL29125_CONF2 0x02
  25. #define ISL29125_CONF3 0x03
  26. #define ISL29125_STATUS 0x08
  27. #define ISL29125_GREEN_DATA 0x09
  28. #define ISL29125_RED_DATA 0x0b
  29. #define ISL29125_BLUE_DATA 0x0d
  30. #define ISL29125_ID 0x7d
  31. #define ISL29125_MODE_MASK GENMASK(2, 0)
  32. #define ISL29125_MODE_PD 0x0
  33. #define ISL29125_MODE_G 0x1
  34. #define ISL29125_MODE_R 0x2
  35. #define ISL29125_MODE_B 0x3
  36. #define ISL29125_MODE_RGB 0x5
  37. #define ISL29125_SENSING_RANGE_0 5722 /* 375 lux full range */
  38. #define ISL29125_SENSING_RANGE_1 152590 /* 10k lux full range */
  39. #define ISL29125_MODE_RANGE BIT(3)
  40. #define ISL29125_STATUS_CONV BIT(1)
  41. struct isl29125_data {
  42. struct i2c_client *client;
  43. u8 conf1;
  44. /* Ensure timestamp is naturally aligned */
  45. struct {
  46. u16 chans[3];
  47. s64 timestamp __aligned(8);
  48. } scan;
  49. };
  50. #define ISL29125_CHANNEL(_color, _si) { \
  51. .type = IIO_INTENSITY, \
  52. .modified = 1, \
  53. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  54. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  55. .channel2 = IIO_MOD_LIGHT_##_color, \
  56. .scan_index = _si, \
  57. .scan_type = { \
  58. .sign = 'u', \
  59. .realbits = 16, \
  60. .storagebits = 16, \
  61. .endianness = IIO_CPU, \
  62. }, \
  63. }
  64. static const struct iio_chan_spec isl29125_channels[] = {
  65. ISL29125_CHANNEL(GREEN, 0),
  66. ISL29125_CHANNEL(RED, 1),
  67. ISL29125_CHANNEL(BLUE, 2),
  68. IIO_CHAN_SOFT_TIMESTAMP(3),
  69. };
  70. static const struct {
  71. u8 mode, data;
  72. } isl29125_regs[] = {
  73. {ISL29125_MODE_G, ISL29125_GREEN_DATA},
  74. {ISL29125_MODE_R, ISL29125_RED_DATA},
  75. {ISL29125_MODE_B, ISL29125_BLUE_DATA},
  76. };
  77. static int isl29125_read_data(struct isl29125_data *data, int si)
  78. {
  79. int tries = 5;
  80. int ret;
  81. ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  82. data->conf1 | isl29125_regs[si].mode);
  83. if (ret < 0)
  84. return ret;
  85. msleep(101);
  86. while (tries--) {
  87. ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS);
  88. if (ret < 0)
  89. goto fail;
  90. if (ret & ISL29125_STATUS_CONV)
  91. break;
  92. msleep(20);
  93. }
  94. if (tries < 0) {
  95. dev_err(&data->client->dev, "data not ready\n");
  96. ret = -EIO;
  97. goto fail;
  98. }
  99. ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data);
  100. fail:
  101. i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1);
  102. return ret;
  103. }
  104. static int isl29125_read_raw(struct iio_dev *indio_dev,
  105. struct iio_chan_spec const *chan,
  106. int *val, int *val2, long mask)
  107. {
  108. struct isl29125_data *data = iio_priv(indio_dev);
  109. int ret;
  110. switch (mask) {
  111. case IIO_CHAN_INFO_RAW:
  112. ret = iio_device_claim_direct_mode(indio_dev);
  113. if (ret)
  114. return ret;
  115. ret = isl29125_read_data(data, chan->scan_index);
  116. iio_device_release_direct_mode(indio_dev);
  117. if (ret < 0)
  118. return ret;
  119. *val = ret;
  120. return IIO_VAL_INT;
  121. case IIO_CHAN_INFO_SCALE:
  122. *val = 0;
  123. if (data->conf1 & ISL29125_MODE_RANGE)
  124. *val2 = ISL29125_SENSING_RANGE_1; /*10k lux full range*/
  125. else
  126. *val2 = ISL29125_SENSING_RANGE_0; /*375 lux full range*/
  127. return IIO_VAL_INT_PLUS_MICRO;
  128. }
  129. return -EINVAL;
  130. }
  131. static int isl29125_write_raw(struct iio_dev *indio_dev,
  132. struct iio_chan_spec const *chan,
  133. int val, int val2, long mask)
  134. {
  135. struct isl29125_data *data = iio_priv(indio_dev);
  136. switch (mask) {
  137. case IIO_CHAN_INFO_SCALE:
  138. if (val != 0)
  139. return -EINVAL;
  140. if (val2 == ISL29125_SENSING_RANGE_1)
  141. data->conf1 |= ISL29125_MODE_RANGE;
  142. else if (val2 == ISL29125_SENSING_RANGE_0)
  143. data->conf1 &= ~ISL29125_MODE_RANGE;
  144. else
  145. return -EINVAL;
  146. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  147. data->conf1);
  148. default:
  149. return -EINVAL;
  150. }
  151. }
  152. static irqreturn_t isl29125_trigger_handler(int irq, void *p)
  153. {
  154. struct iio_poll_func *pf = p;
  155. struct iio_dev *indio_dev = pf->indio_dev;
  156. struct isl29125_data *data = iio_priv(indio_dev);
  157. int i, j = 0;
  158. iio_for_each_active_channel(indio_dev, i) {
  159. int ret = i2c_smbus_read_word_data(data->client,
  160. isl29125_regs[i].data);
  161. if (ret < 0)
  162. goto done;
  163. data->scan.chans[j++] = ret;
  164. }
  165. iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
  166. iio_get_time_ns(indio_dev));
  167. done:
  168. iio_trigger_notify_done(indio_dev->trig);
  169. return IRQ_HANDLED;
  170. }
  171. static IIO_CONST_ATTR(scale_available, "0.005722 0.152590");
  172. static struct attribute *isl29125_attributes[] = {
  173. &iio_const_attr_scale_available.dev_attr.attr,
  174. NULL
  175. };
  176. static const struct attribute_group isl29125_attribute_group = {
  177. .attrs = isl29125_attributes,
  178. };
  179. static const struct iio_info isl29125_info = {
  180. .read_raw = isl29125_read_raw,
  181. .write_raw = isl29125_write_raw,
  182. .attrs = &isl29125_attribute_group,
  183. };
  184. static int isl29125_buffer_postenable(struct iio_dev *indio_dev)
  185. {
  186. struct isl29125_data *data = iio_priv(indio_dev);
  187. data->conf1 |= ISL29125_MODE_RGB;
  188. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  189. data->conf1);
  190. }
  191. static int isl29125_buffer_predisable(struct iio_dev *indio_dev)
  192. {
  193. struct isl29125_data *data = iio_priv(indio_dev);
  194. data->conf1 &= ~ISL29125_MODE_MASK;
  195. data->conf1 |= ISL29125_MODE_PD;
  196. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  197. data->conf1);
  198. }
  199. static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = {
  200. .postenable = isl29125_buffer_postenable,
  201. .predisable = isl29125_buffer_predisable,
  202. };
  203. static int isl29125_probe(struct i2c_client *client)
  204. {
  205. struct isl29125_data *data;
  206. struct iio_dev *indio_dev;
  207. int ret;
  208. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  209. if (indio_dev == NULL)
  210. return -ENOMEM;
  211. data = iio_priv(indio_dev);
  212. i2c_set_clientdata(client, indio_dev);
  213. data->client = client;
  214. indio_dev->info = &isl29125_info;
  215. indio_dev->name = ISL29125_DRV_NAME;
  216. indio_dev->channels = isl29125_channels;
  217. indio_dev->num_channels = ARRAY_SIZE(isl29125_channels);
  218. indio_dev->modes = INDIO_DIRECT_MODE;
  219. ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID);
  220. if (ret < 0)
  221. return ret;
  222. if (ret != ISL29125_ID)
  223. return -ENODEV;
  224. data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE;
  225. ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  226. data->conf1);
  227. if (ret < 0)
  228. return ret;
  229. ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0);
  230. if (ret < 0)
  231. return ret;
  232. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  233. isl29125_trigger_handler, &isl29125_buffer_setup_ops);
  234. if (ret < 0)
  235. return ret;
  236. ret = iio_device_register(indio_dev);
  237. if (ret < 0)
  238. goto buffer_cleanup;
  239. return 0;
  240. buffer_cleanup:
  241. iio_triggered_buffer_cleanup(indio_dev);
  242. return ret;
  243. }
  244. static int isl29125_powerdown(struct isl29125_data *data)
  245. {
  246. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  247. (data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD);
  248. }
  249. static void isl29125_remove(struct i2c_client *client)
  250. {
  251. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  252. iio_device_unregister(indio_dev);
  253. iio_triggered_buffer_cleanup(indio_dev);
  254. isl29125_powerdown(iio_priv(indio_dev));
  255. }
  256. static int isl29125_suspend(struct device *dev)
  257. {
  258. struct isl29125_data *data = iio_priv(i2c_get_clientdata(
  259. to_i2c_client(dev)));
  260. return isl29125_powerdown(data);
  261. }
  262. static int isl29125_resume(struct device *dev)
  263. {
  264. struct isl29125_data *data = iio_priv(i2c_get_clientdata(
  265. to_i2c_client(dev)));
  266. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  267. data->conf1);
  268. }
  269. static DEFINE_SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend,
  270. isl29125_resume);
  271. static const struct i2c_device_id isl29125_id[] = {
  272. { "isl29125" },
  273. { }
  274. };
  275. MODULE_DEVICE_TABLE(i2c, isl29125_id);
  276. static struct i2c_driver isl29125_driver = {
  277. .driver = {
  278. .name = ISL29125_DRV_NAME,
  279. .pm = pm_sleep_ptr(&isl29125_pm_ops),
  280. },
  281. .probe = isl29125_probe,
  282. .remove = isl29125_remove,
  283. .id_table = isl29125_id,
  284. };
  285. module_i2c_driver(isl29125_driver);
  286. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  287. MODULE_DESCRIPTION("ISL29125 RGB light sensor driver");
  288. MODULE_LICENSE("GPL");