isl29125.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_MODE_RANGE BIT(3)
  41. #define ISL29125_STATUS_CONV BIT(1)
  42. struct isl29125_data {
  43. struct i2c_client *client;
  44. struct mutex lock;
  45. u8 conf1;
  46. u16 buffer[8]; /* 3x 16-bit, padding, 8 bytes timestamp */
  47. };
  48. #define ISL29125_CHANNEL(_color, _si) { \
  49. .type = IIO_INTENSITY, \
  50. .modified = 1, \
  51. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  52. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  53. .channel2 = IIO_MOD_LIGHT_##_color, \
  54. .scan_index = _si, \
  55. .scan_type = { \
  56. .sign = 'u', \
  57. .realbits = 16, \
  58. .storagebits = 16, \
  59. .endianness = IIO_CPU, \
  60. }, \
  61. }
  62. static const struct iio_chan_spec isl29125_channels[] = {
  63. ISL29125_CHANNEL(GREEN, 0),
  64. ISL29125_CHANNEL(RED, 1),
  65. ISL29125_CHANNEL(BLUE, 2),
  66. IIO_CHAN_SOFT_TIMESTAMP(3),
  67. };
  68. static const struct {
  69. u8 mode, data;
  70. } isl29125_regs[] = {
  71. {ISL29125_MODE_G, ISL29125_GREEN_DATA},
  72. {ISL29125_MODE_R, ISL29125_RED_DATA},
  73. {ISL29125_MODE_B, ISL29125_BLUE_DATA},
  74. };
  75. static int isl29125_read_data(struct isl29125_data *data, int si)
  76. {
  77. int tries = 5;
  78. int ret;
  79. ret = i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  80. data->conf1 | isl29125_regs[si].mode);
  81. if (ret < 0)
  82. return ret;
  83. msleep(101);
  84. while (tries--) {
  85. ret = i2c_smbus_read_byte_data(data->client, ISL29125_STATUS);
  86. if (ret < 0)
  87. goto fail;
  88. if (ret & ISL29125_STATUS_CONV)
  89. break;
  90. msleep(20);
  91. }
  92. if (tries < 0) {
  93. dev_err(&data->client->dev, "data not ready\n");
  94. ret = -EIO;
  95. goto fail;
  96. }
  97. ret = i2c_smbus_read_word_data(data->client, isl29125_regs[si].data);
  98. fail:
  99. i2c_smbus_write_byte_data(data->client, ISL29125_CONF1, data->conf1);
  100. return ret;
  101. }
  102. static int isl29125_read_raw(struct iio_dev *indio_dev,
  103. struct iio_chan_spec const *chan,
  104. int *val, int *val2, long mask)
  105. {
  106. struct isl29125_data *data = iio_priv(indio_dev);
  107. int ret;
  108. switch (mask) {
  109. case IIO_CHAN_INFO_RAW:
  110. if (iio_buffer_enabled(indio_dev))
  111. return -EBUSY;
  112. mutex_lock(&data->lock);
  113. ret = isl29125_read_data(data, chan->scan_index);
  114. mutex_unlock(&data->lock);
  115. if (ret < 0)
  116. return ret;
  117. *val = ret;
  118. return IIO_VAL_INT;
  119. case IIO_CHAN_INFO_SCALE:
  120. *val = 0;
  121. if (data->conf1 & ISL29125_MODE_RANGE)
  122. *val2 = 152590; /* 10k lux full range */
  123. else
  124. *val2 = 5722; /* 375 lux full range */
  125. return IIO_VAL_INT_PLUS_MICRO;
  126. }
  127. return -EINVAL;
  128. }
  129. static int isl29125_write_raw(struct iio_dev *indio_dev,
  130. struct iio_chan_spec const *chan,
  131. int val, int val2, long mask)
  132. {
  133. struct isl29125_data *data = iio_priv(indio_dev);
  134. switch (mask) {
  135. case IIO_CHAN_INFO_SCALE:
  136. if (val != 0)
  137. return -EINVAL;
  138. if (val2 == 152590)
  139. data->conf1 |= ISL29125_MODE_RANGE;
  140. else if (val2 == 5722)
  141. data->conf1 &= ~ISL29125_MODE_RANGE;
  142. else
  143. return -EINVAL;
  144. return i2c_smbus_write_byte_data(data->client, ISL29125_CONF1,
  145. data->conf1);
  146. default:
  147. return -EINVAL;
  148. }
  149. }
  150. static irqreturn_t isl29125_trigger_handler(int irq, void *p)
  151. {
  152. struct iio_poll_func *pf = p;
  153. struct iio_dev *indio_dev = pf->indio_dev;
  154. struct isl29125_data *data = iio_priv(indio_dev);
  155. int i, j = 0;
  156. for_each_set_bit(i, indio_dev->active_scan_mask,
  157. indio_dev->masklength) {
  158. int ret = i2c_smbus_read_word_data(data->client,
  159. isl29125_regs[i].data);
  160. if (ret < 0)
  161. goto done;
  162. data->buffer[j++] = ret;
  163. }
  164. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  165. iio_get_time_ns());
  166. done:
  167. iio_trigger_notify_done(indio_dev->trig);
  168. return IRQ_HANDLED;
  169. }
  170. static IIO_CONST_ATTR(scale_available, "0.005722 0.152590");
  171. static struct attribute *isl29125_attributes[] = {
  172. &iio_const_attr_scale_available.dev_attr.attr,
  173. NULL
  174. };
  175. static const struct attribute_group isl29125_attribute_group = {
  176. .attrs = isl29125_attributes,
  177. };
  178. static const struct iio_info isl29125_info = {
  179. .read_raw = isl29125_read_raw,
  180. .write_raw = isl29125_write_raw,
  181. .attrs = &isl29125_attribute_group,
  182. .driver_module = THIS_MODULE,
  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. mutex_init(&data->lock);
  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");