tsl4531.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * tsl4531.c - Support for TAOS TSL4531 ambient light sensor
  3. *
  4. * Copyright 2013 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. * IIO driver for the TSL4531x family
  11. * TSL45311/TSL45313: 7-bit I2C slave address 0x39
  12. * TSL45315/TSL45317: 7-bit I2C slave address 0x29
  13. *
  14. * TODO: single cycle measurement
  15. */
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #define TSL4531_DRV_NAME "tsl4531"
  23. #define TSL4531_COMMAND BIT(7)
  24. #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00)
  25. #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01)
  26. #define TSL4531_DATA (TSL4531_COMMAND | 0x04)
  27. #define TSL4531_ID (TSL4531_COMMAND | 0x0a)
  28. /* operating modes in control register */
  29. #define TSL4531_MODE_POWERDOWN 0x00
  30. #define TSL4531_MODE_SINGLE_ADC 0x02
  31. #define TSL4531_MODE_NORMAL 0x03
  32. /* integration time control in config register */
  33. #define TSL4531_TCNTRL_400MS 0x00
  34. #define TSL4531_TCNTRL_200MS 0x01
  35. #define TSL4531_TCNTRL_100MS 0x02
  36. /* part number in id register */
  37. #define TSL45311_ID 0x8
  38. #define TSL45313_ID 0x9
  39. #define TSL45315_ID 0xa
  40. #define TSL45317_ID 0xb
  41. #define TSL4531_ID_SHIFT 4
  42. struct tsl4531_data {
  43. struct i2c_client *client;
  44. struct mutex lock;
  45. int int_time;
  46. };
  47. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4");
  48. static struct attribute *tsl4531_attributes[] = {
  49. &iio_const_attr_integration_time_available.dev_attr.attr,
  50. NULL
  51. };
  52. static const struct attribute_group tsl4531_attribute_group = {
  53. .attrs = tsl4531_attributes,
  54. };
  55. static const struct iio_chan_spec tsl4531_channels[] = {
  56. {
  57. .type = IIO_LIGHT,
  58. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  59. BIT(IIO_CHAN_INFO_SCALE) |
  60. BIT(IIO_CHAN_INFO_INT_TIME)
  61. }
  62. };
  63. static int tsl4531_read_raw(struct iio_dev *indio_dev,
  64. struct iio_chan_spec const *chan,
  65. int *val, int *val2, long mask)
  66. {
  67. struct tsl4531_data *data = iio_priv(indio_dev);
  68. int ret;
  69. switch (mask) {
  70. case IIO_CHAN_INFO_RAW:
  71. ret = i2c_smbus_read_word_data(data->client,
  72. TSL4531_DATA);
  73. if (ret < 0)
  74. return ret;
  75. *val = ret;
  76. return IIO_VAL_INT;
  77. case IIO_CHAN_INFO_SCALE:
  78. /* 0.. 1x, 1 .. 2x, 2 .. 4x */
  79. *val = 1 << data->int_time;
  80. return IIO_VAL_INT;
  81. case IIO_CHAN_INFO_INT_TIME:
  82. if (data->int_time == 0)
  83. *val2 = 400000;
  84. else if (data->int_time == 1)
  85. *val2 = 200000;
  86. else if (data->int_time == 2)
  87. *val2 = 100000;
  88. else
  89. return -EINVAL;
  90. *val = 0;
  91. return IIO_VAL_INT_PLUS_MICRO;
  92. default:
  93. return -EINVAL;
  94. }
  95. }
  96. static int tsl4531_write_raw(struct iio_dev *indio_dev,
  97. struct iio_chan_spec const *chan,
  98. int val, int val2, long mask)
  99. {
  100. struct tsl4531_data *data = iio_priv(indio_dev);
  101. int int_time, ret;
  102. switch (mask) {
  103. case IIO_CHAN_INFO_INT_TIME:
  104. if (val != 0)
  105. return -EINVAL;
  106. if (val2 == 400000)
  107. int_time = 0;
  108. else if (val2 == 200000)
  109. int_time = 1;
  110. else if (val2 == 100000)
  111. int_time = 2;
  112. else
  113. return -EINVAL;
  114. mutex_lock(&data->lock);
  115. ret = i2c_smbus_write_byte_data(data->client,
  116. TSL4531_CONFIG, int_time);
  117. if (ret >= 0)
  118. data->int_time = int_time;
  119. mutex_unlock(&data->lock);
  120. return ret;
  121. default:
  122. return -EINVAL;
  123. }
  124. }
  125. static const struct iio_info tsl4531_info = {
  126. .read_raw = tsl4531_read_raw,
  127. .write_raw = tsl4531_write_raw,
  128. .attrs = &tsl4531_attribute_group,
  129. .driver_module = THIS_MODULE,
  130. };
  131. static int tsl4531_check_id(struct i2c_client *client)
  132. {
  133. int ret = i2c_smbus_read_byte_data(client, TSL4531_ID);
  134. if (ret < 0)
  135. return ret;
  136. switch (ret >> TSL4531_ID_SHIFT) {
  137. case TSL45311_ID:
  138. case TSL45313_ID:
  139. case TSL45315_ID:
  140. case TSL45317_ID:
  141. return 0;
  142. default:
  143. return -ENODEV;
  144. }
  145. }
  146. static int tsl4531_probe(struct i2c_client *client,
  147. const struct i2c_device_id *id)
  148. {
  149. struct tsl4531_data *data;
  150. struct iio_dev *indio_dev;
  151. int ret;
  152. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  153. if (!indio_dev)
  154. return -ENOMEM;
  155. data = iio_priv(indio_dev);
  156. i2c_set_clientdata(client, indio_dev);
  157. data->client = client;
  158. mutex_init(&data->lock);
  159. ret = tsl4531_check_id(client);
  160. if (ret) {
  161. dev_err(&client->dev, "no TSL4531 sensor\n");
  162. return ret;
  163. }
  164. ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONTROL,
  165. TSL4531_MODE_NORMAL);
  166. if (ret < 0)
  167. return ret;
  168. ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONFIG,
  169. TSL4531_TCNTRL_400MS);
  170. if (ret < 0)
  171. return ret;
  172. indio_dev->dev.parent = &client->dev;
  173. indio_dev->info = &tsl4531_info;
  174. indio_dev->channels = tsl4531_channels;
  175. indio_dev->num_channels = ARRAY_SIZE(tsl4531_channels);
  176. indio_dev->name = TSL4531_DRV_NAME;
  177. indio_dev->modes = INDIO_DIRECT_MODE;
  178. return iio_device_register(indio_dev);
  179. }
  180. static int tsl4531_powerdown(struct i2c_client *client)
  181. {
  182. return i2c_smbus_write_byte_data(client, TSL4531_CONTROL,
  183. TSL4531_MODE_POWERDOWN);
  184. }
  185. static int tsl4531_remove(struct i2c_client *client)
  186. {
  187. iio_device_unregister(i2c_get_clientdata(client));
  188. tsl4531_powerdown(client);
  189. return 0;
  190. }
  191. #ifdef CONFIG_PM_SLEEP
  192. static int tsl4531_suspend(struct device *dev)
  193. {
  194. return tsl4531_powerdown(to_i2c_client(dev));
  195. }
  196. static int tsl4531_resume(struct device *dev)
  197. {
  198. return i2c_smbus_write_byte_data(to_i2c_client(dev), TSL4531_CONTROL,
  199. TSL4531_MODE_NORMAL);
  200. }
  201. static SIMPLE_DEV_PM_OPS(tsl4531_pm_ops, tsl4531_suspend, tsl4531_resume);
  202. #define TSL4531_PM_OPS (&tsl4531_pm_ops)
  203. #else
  204. #define TSL4531_PM_OPS NULL
  205. #endif
  206. static const struct i2c_device_id tsl4531_id[] = {
  207. { "tsl4531", 0 },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(i2c, tsl4531_id);
  211. static struct i2c_driver tsl4531_driver = {
  212. .driver = {
  213. .name = TSL4531_DRV_NAME,
  214. .pm = TSL4531_PM_OPS,
  215. },
  216. .probe = tsl4531_probe,
  217. .remove = tsl4531_remove,
  218. .id_table = tsl4531_id,
  219. };
  220. module_i2c_driver(tsl4531_driver);
  221. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  222. MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver");
  223. MODULE_LICENSE("GPL");