isl29125.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. .driver_module = THIS_MODULE,
  184. };
  185. static int isl29125_buffer_preenable(struct iio_dev *indio_dev)
  186. {
  187. struct isl29125_data *data = iio_priv(indio_dev);
  188. data->conf1 |= ISL29125_MODE_RGB;
  189. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  190. data->conf1);
  191. }
  192. static int isl29125_buffer_predisable(struct iio_dev *indio_dev)
  193. {
  194. struct isl29125_data *data = iio_priv(indio_dev);
  195. int ret;
  196. ret = iio_triggered_buffer_predisable(indio_dev);
  197. if (ret < 0)
  198. return ret;
  199. data->conf1 &= ~ISL29125_MODE_MASK;
  200. data->conf1 |= ISL29125_MODE_PD;
  201. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  202. data->conf1);
  203. }
  204. static const struct iio_buffer_setup_ops isl29125_buffer_setup_ops = {
  205. .preenable = isl29125_buffer_preenable,
  206. .postenable = &iio_triggered_buffer_postenable,
  207. .predisable = isl29125_buffer_predisable,
  208. };
  209. static int isl29125_probe(struct i2c_client *client,
  210. const struct i2c_device_id *id)
  211. {
  212. struct isl29125_data *data;
  213. struct iio_dev *indio_dev;
  214. int ret;
  215. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  216. if (indio_dev == NULL)
  217. return -ENOMEM;
  218. data = iio_priv(indio_dev);
  219. i2c_set_clientdata(client, indio_dev);
  220. data->client = client;
  221. indio_dev->dev.parent = &client->dev;
  222. indio_dev->info = &isl29125_info;
  223. indio_dev->name = ISL29125_DRV_NAME;
  224. indio_dev->channels = isl29125_channels;
  225. indio_dev->num_channels = ARRAY_SIZE(isl29125_channels);
  226. indio_dev->modes = INDIO_DIRECT_MODE;
  227. ret = i2c_smbus_read_byte_data(data->client, ISL29125_DEVICE_ID);
  228. if (ret < 0)
  229. return ret;
  230. if (ret != ISL29125_ID)
  231. return -ENODEV;
  232. data->conf1 = ISL29125_MODE_PD | ISL29125_MODE_RANGE;
  233. ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  234. data->conf1);
  235. if (ret < 0)
  236. return ret;
  237. ret = i2c_smbus_write_byte_data(data->client, ISL29125_STATUS, 0);
  238. if (ret < 0)
  239. return ret;
  240. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  241. isl29125_trigger_handler, &isl29125_buffer_setup_ops);
  242. if (ret < 0)
  243. return ret;
  244. ret = iio_device_register(indio_dev);
  245. if (ret < 0)
  246. goto buffer_cleanup;
  247. return 0;
  248. buffer_cleanup:
  249. iio_triggered_buffer_cleanup(indio_dev);
  250. return ret;
  251. }
  252. static int isl29125_powerdown(struct isl29125_data *data)
  253. {
  254. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  255. (data->conf1 & ~ISL29125_MODE_MASK) | ISL29125_MODE_PD);
  256. }
  257. static int isl29125_remove(struct i2c_client *client)
  258. {
  259. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  260. iio_device_unregister(indio_dev);
  261. iio_triggered_buffer_cleanup(indio_dev);
  262. isl29125_powerdown(iio_priv(indio_dev));
  263. return 0;
  264. }
  265. #ifdef CONFIG_PM_SLEEP
  266. static int isl29125_suspend(struct device *dev)
  267. {
  268. struct isl29125_data *data = iio_priv(i2c_get_clientdata(
  269. to_i2c_client(dev)));
  270. return isl29125_powerdown(data);
  271. }
  272. static int isl29125_resume(struct device *dev)
  273. {
  274. struct isl29125_data *data = iio_priv(i2c_get_clientdata(
  275. to_i2c_client(dev)));
  276. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  277. data->conf1);
  278. }
  279. #endif
  280. static SIMPLE_DEV_PM_OPS(isl29125_pm_ops, isl29125_suspend, isl29125_resume);
  281. static const struct i2c_device_id isl29125_id[] = {
  282. { "isl29125", 0 },
  283. { }
  284. };
  285. MODULE_DEVICE_TABLE(i2c, isl29125_id);
  286. static struct i2c_driver isl29125_driver = {
  287. .driver = {
  288. .name = ISL29125_DRV_NAME,
  289. .pm = &isl29125_pm_ops,
  290. },
  291. .probe = isl29125_probe,
  292. .remove = isl29125_remove,
  293. .id_table = isl29125_id,
  294. };
  295. module_i2c_driver(isl29125_driver);
  296. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  297. MODULE_DESCRIPTION("ISL29125 RGB light sensor driver");
  298. MODULE_LICENSE("GPL");