cm32181.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (C) 2013 Capella Microsystems Inc.
  3. * Author: Kevin Tsai <ktsai@capellamicro.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2, as published
  7. * by the Free Software Foundation.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mutex.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/events.h>
  19. #include <linux/init.h>
  20. /* Registers Address */
  21. #define CM32181_REG_ADDR_CMD 0x00
  22. #define CM32181_REG_ADDR_ALS 0x04
  23. #define CM32181_REG_ADDR_STATUS 0x06
  24. #define CM32181_REG_ADDR_ID 0x07
  25. /* Number of Configurable Registers */
  26. #define CM32181_CONF_REG_NUM 0x01
  27. /* CMD register */
  28. #define CM32181_CMD_ALS_ENABLE 0x00
  29. #define CM32181_CMD_ALS_DISABLE 0x01
  30. #define CM32181_CMD_ALS_INT_EN 0x02
  31. #define CM32181_CMD_ALS_IT_SHIFT 6
  32. #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT)
  33. #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT)
  34. #define CM32181_CMD_ALS_SM_SHIFT 11
  35. #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT)
  36. #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
  37. #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */
  38. #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */
  39. #define CM32181_CALIBSCALE_DEFAULT 1000
  40. #define CM32181_CALIBSCALE_RESOLUTION 1000
  41. #define MLUX_PER_LUX 1000
  42. static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
  43. CM32181_REG_ADDR_CMD,
  44. };
  45. static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
  46. static const int als_it_value[] = {25000, 50000, 100000, 200000, 400000,
  47. 800000};
  48. struct cm32181_chip {
  49. struct i2c_client *client;
  50. struct mutex lock;
  51. u16 conf_regs[CM32181_CONF_REG_NUM];
  52. int calibscale;
  53. };
  54. /**
  55. * cm32181_reg_init() - Initialize CM32181 registers
  56. * @cm32181: pointer of struct cm32181.
  57. *
  58. * Initialize CM32181 ambient light sensor register to default values.
  59. *
  60. * Return: 0 for success; otherwise for error code.
  61. */
  62. static int cm32181_reg_init(struct cm32181_chip *cm32181)
  63. {
  64. struct i2c_client *client = cm32181->client;
  65. int i;
  66. s32 ret;
  67. ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
  68. if (ret < 0)
  69. return ret;
  70. /* check device ID */
  71. if ((ret & 0xFF) != 0x81)
  72. return -ENODEV;
  73. /* Default Values */
  74. cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE |
  75. CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
  76. cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
  77. /* Initialize registers*/
  78. for (i = 0; i < CM32181_CONF_REG_NUM; i++) {
  79. ret = i2c_smbus_write_word_data(client, cm32181_reg[i],
  80. cm32181->conf_regs[i]);
  81. if (ret < 0)
  82. return ret;
  83. }
  84. return 0;
  85. }
  86. /**
  87. * cm32181_read_als_it() - Get sensor integration time (ms)
  88. * @cm32181: pointer of struct cm32181
  89. * @val2: pointer of int to load the als_it value.
  90. *
  91. * Report the current integartion time by millisecond.
  92. *
  93. * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  94. */
  95. static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
  96. {
  97. u16 als_it;
  98. int i;
  99. als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
  100. als_it &= CM32181_CMD_ALS_IT_MASK;
  101. als_it >>= CM32181_CMD_ALS_IT_SHIFT;
  102. for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) {
  103. if (als_it == als_it_bits[i]) {
  104. *val2 = als_it_value[i];
  105. return IIO_VAL_INT_PLUS_MICRO;
  106. }
  107. }
  108. return -EINVAL;
  109. }
  110. /**
  111. * cm32181_write_als_it() - Write sensor integration time
  112. * @cm32181: pointer of struct cm32181.
  113. * @val: integration time by millisecond.
  114. *
  115. * Convert integration time (ms) to sensor value.
  116. *
  117. * Return: i2c_smbus_write_word_data command return value.
  118. */
  119. static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
  120. {
  121. struct i2c_client *client = cm32181->client;
  122. u16 als_it;
  123. int ret, i, n;
  124. n = ARRAY_SIZE(als_it_value);
  125. for (i = 0; i < n; i++)
  126. if (val <= als_it_value[i])
  127. break;
  128. if (i >= n)
  129. i = n - 1;
  130. als_it = als_it_bits[i];
  131. als_it <<= CM32181_CMD_ALS_IT_SHIFT;
  132. mutex_lock(&cm32181->lock);
  133. cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
  134. ~CM32181_CMD_ALS_IT_MASK;
  135. cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
  136. als_it;
  137. ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
  138. cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
  139. mutex_unlock(&cm32181->lock);
  140. return ret;
  141. }
  142. /**
  143. * cm32181_get_lux() - report current lux value
  144. * @cm32181: pointer of struct cm32181.
  145. *
  146. * Convert sensor raw data to lux. It depends on integration
  147. * time and claibscale variable.
  148. *
  149. * Return: Positive value is lux, otherwise is error code.
  150. */
  151. static int cm32181_get_lux(struct cm32181_chip *cm32181)
  152. {
  153. struct i2c_client *client = cm32181->client;
  154. int ret;
  155. int als_it;
  156. unsigned long lux;
  157. ret = cm32181_read_als_it(cm32181, &als_it);
  158. if (ret < 0)
  159. return -EINVAL;
  160. lux = CM32181_MLUX_PER_BIT;
  161. lux *= CM32181_MLUX_PER_BIT_BASE_IT;
  162. lux /= als_it;
  163. ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
  164. if (ret < 0)
  165. return ret;
  166. lux *= ret;
  167. lux *= cm32181->calibscale;
  168. lux /= CM32181_CALIBSCALE_RESOLUTION;
  169. lux /= MLUX_PER_LUX;
  170. if (lux > 0xFFFF)
  171. lux = 0xFFFF;
  172. return lux;
  173. }
  174. static int cm32181_read_raw(struct iio_dev *indio_dev,
  175. struct iio_chan_spec const *chan,
  176. int *val, int *val2, long mask)
  177. {
  178. struct cm32181_chip *cm32181 = iio_priv(indio_dev);
  179. int ret;
  180. switch (mask) {
  181. case IIO_CHAN_INFO_PROCESSED:
  182. ret = cm32181_get_lux(cm32181);
  183. if (ret < 0)
  184. return ret;
  185. *val = ret;
  186. return IIO_VAL_INT;
  187. case IIO_CHAN_INFO_CALIBSCALE:
  188. *val = cm32181->calibscale;
  189. return IIO_VAL_INT;
  190. case IIO_CHAN_INFO_INT_TIME:
  191. ret = cm32181_read_als_it(cm32181, val2);
  192. return ret;
  193. }
  194. return -EINVAL;
  195. }
  196. static int cm32181_write_raw(struct iio_dev *indio_dev,
  197. struct iio_chan_spec const *chan,
  198. int val, int val2, long mask)
  199. {
  200. struct cm32181_chip *cm32181 = iio_priv(indio_dev);
  201. int ret;
  202. switch (mask) {
  203. case IIO_CHAN_INFO_CALIBSCALE:
  204. cm32181->calibscale = val;
  205. return val;
  206. case IIO_CHAN_INFO_INT_TIME:
  207. ret = cm32181_write_als_it(cm32181, val2);
  208. return ret;
  209. }
  210. return -EINVAL;
  211. }
  212. /**
  213. * cm32181_get_it_available() - Get available ALS IT value
  214. * @dev: pointer of struct device.
  215. * @attr: pointer of struct device_attribute.
  216. * @buf: pointer of return string buffer.
  217. *
  218. * Display the available integration time values by millisecond.
  219. *
  220. * Return: string length.
  221. */
  222. static ssize_t cm32181_get_it_available(struct device *dev,
  223. struct device_attribute *attr, char *buf)
  224. {
  225. int i, n, len;
  226. n = ARRAY_SIZE(als_it_value);
  227. for (i = 0, len = 0; i < n; i++)
  228. len += sprintf(buf + len, "0.%06u ", als_it_value[i]);
  229. return len + sprintf(buf + len, "\n");
  230. }
  231. static const struct iio_chan_spec cm32181_channels[] = {
  232. {
  233. .type = IIO_LIGHT,
  234. .info_mask_separate =
  235. BIT(IIO_CHAN_INFO_PROCESSED) |
  236. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  237. BIT(IIO_CHAN_INFO_INT_TIME),
  238. }
  239. };
  240. static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
  241. S_IRUGO, cm32181_get_it_available, NULL, 0);
  242. static struct attribute *cm32181_attributes[] = {
  243. &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
  244. NULL,
  245. };
  246. static const struct attribute_group cm32181_attribute_group = {
  247. .attrs = cm32181_attributes
  248. };
  249. static const struct iio_info cm32181_info = {
  250. .driver_module = THIS_MODULE,
  251. .read_raw = &cm32181_read_raw,
  252. .write_raw = &cm32181_write_raw,
  253. .attrs = &cm32181_attribute_group,
  254. };
  255. static int cm32181_probe(struct i2c_client *client,
  256. const struct i2c_device_id *id)
  257. {
  258. struct cm32181_chip *cm32181;
  259. struct iio_dev *indio_dev;
  260. int ret;
  261. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181));
  262. if (!indio_dev) {
  263. dev_err(&client->dev, "devm_iio_device_alloc failed\n");
  264. return -ENOMEM;
  265. }
  266. cm32181 = iio_priv(indio_dev);
  267. i2c_set_clientdata(client, indio_dev);
  268. cm32181->client = client;
  269. mutex_init(&cm32181->lock);
  270. indio_dev->dev.parent = &client->dev;
  271. indio_dev->channels = cm32181_channels;
  272. indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
  273. indio_dev->info = &cm32181_info;
  274. indio_dev->name = id->name;
  275. indio_dev->modes = INDIO_DIRECT_MODE;
  276. ret = cm32181_reg_init(cm32181);
  277. if (ret) {
  278. dev_err(&client->dev,
  279. "%s: register init failed\n",
  280. __func__);
  281. return ret;
  282. }
  283. ret = iio_device_register(indio_dev);
  284. if (ret) {
  285. dev_err(&client->dev,
  286. "%s: regist device failed\n",
  287. __func__);
  288. return ret;
  289. }
  290. return 0;
  291. }
  292. static int cm32181_remove(struct i2c_client *client)
  293. {
  294. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  295. iio_device_unregister(indio_dev);
  296. return 0;
  297. }
  298. static const struct i2c_device_id cm32181_id[] = {
  299. { "cm32181", 0 },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(i2c, cm32181_id);
  303. static const struct of_device_id cm32181_of_match[] = {
  304. { .compatible = "capella,cm32181" },
  305. { }
  306. };
  307. static struct i2c_driver cm32181_driver = {
  308. .driver = {
  309. .name = "cm32181",
  310. .of_match_table = of_match_ptr(cm32181_of_match),
  311. .owner = THIS_MODULE,
  312. },
  313. .id_table = cm32181_id,
  314. .probe = cm32181_probe,
  315. .remove = cm32181_remove,
  316. };
  317. module_i2c_driver(cm32181_driver);
  318. MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
  319. MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
  320. MODULE_LICENSE("GPL");