isl29125.c 8.6 KB

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