apds9802als.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * apds9802als.c - apds9802 ALS Driver
  3. *
  4. * Copyright (C) 2009 Intel Corp
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/err.h>
  27. #include <linux/delay.h>
  28. #include <linux/mutex.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/pm_runtime.h>
  31. #define ALS_MIN_RANGE_VAL 1
  32. #define ALS_MAX_RANGE_VAL 2
  33. #define POWER_STA_ENABLE 1
  34. #define POWER_STA_DISABLE 0
  35. #define DRIVER_NAME "apds9802als"
  36. struct als_data {
  37. struct mutex mutex;
  38. };
  39. static ssize_t als_sensing_range_show(struct device *dev,
  40. struct device_attribute *attr, char *buf)
  41. {
  42. struct i2c_client *client = to_i2c_client(dev);
  43. int val;
  44. val = i2c_smbus_read_byte_data(client, 0x81);
  45. if (val < 0)
  46. return val;
  47. if (val & 1)
  48. return sprintf(buf, "4095\n");
  49. else
  50. return sprintf(buf, "65535\n");
  51. }
  52. static int als_wait_for_data_ready(struct device *dev)
  53. {
  54. struct i2c_client *client = to_i2c_client(dev);
  55. int ret;
  56. int retry = 10;
  57. do {
  58. msleep(30);
  59. ret = i2c_smbus_read_byte_data(client, 0x86);
  60. } while (!(ret & 0x80) && retry--);
  61. if (retry < 0) {
  62. dev_warn(dev, "timeout waiting for data ready\n");
  63. return -ETIMEDOUT;
  64. }
  65. return 0;
  66. }
  67. static ssize_t als_lux0_input_data_show(struct device *dev,
  68. struct device_attribute *attr, char *buf)
  69. {
  70. struct i2c_client *client = to_i2c_client(dev);
  71. struct als_data *data = i2c_get_clientdata(client);
  72. int ret_val;
  73. int temp;
  74. /* Protect against parallel reads */
  75. pm_runtime_get_sync(dev);
  76. mutex_lock(&data->mutex);
  77. /* clear EOC interrupt status */
  78. i2c_smbus_write_byte(client, 0x40);
  79. /* start measurement */
  80. temp = i2c_smbus_read_byte_data(client, 0x81);
  81. i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
  82. ret_val = als_wait_for_data_ready(dev);
  83. if (ret_val < 0)
  84. goto failed;
  85. temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
  86. if (temp < 0) {
  87. ret_val = temp;
  88. goto failed;
  89. }
  90. ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
  91. if (ret_val < 0)
  92. goto failed;
  93. mutex_unlock(&data->mutex);
  94. pm_runtime_put_sync(dev);
  95. temp = (ret_val << 8) | temp;
  96. return sprintf(buf, "%d\n", temp);
  97. failed:
  98. mutex_unlock(&data->mutex);
  99. pm_runtime_put_sync(dev);
  100. return ret_val;
  101. }
  102. static ssize_t als_sensing_range_store(struct device *dev,
  103. struct device_attribute *attr, const char *buf, size_t count)
  104. {
  105. struct i2c_client *client = to_i2c_client(dev);
  106. struct als_data *data = i2c_get_clientdata(client);
  107. int ret_val;
  108. unsigned long val;
  109. ret_val = kstrtoul(buf, 10, &val);
  110. if (ret_val)
  111. return ret_val;
  112. if (val < 4096)
  113. val = 1;
  114. else if (val < 65536)
  115. val = 2;
  116. else
  117. return -ERANGE;
  118. pm_runtime_get_sync(dev);
  119. /* Make sure nobody else reads/modifies/writes 0x81 while we
  120. are active */
  121. mutex_lock(&data->mutex);
  122. ret_val = i2c_smbus_read_byte_data(client, 0x81);
  123. if (ret_val < 0)
  124. goto fail;
  125. /* Reset the bits before setting them */
  126. ret_val = ret_val & 0xFA;
  127. if (val == 1) /* Setting detection range up to 4k LUX */
  128. ret_val = (ret_val | 0x01);
  129. else /* Setting detection range up to 64k LUX*/
  130. ret_val = (ret_val | 0x00);
  131. ret_val = i2c_smbus_write_byte_data(client, 0x81, ret_val);
  132. if (ret_val >= 0) {
  133. /* All OK */
  134. mutex_unlock(&data->mutex);
  135. pm_runtime_put_sync(dev);
  136. return count;
  137. }
  138. fail:
  139. mutex_unlock(&data->mutex);
  140. pm_runtime_put_sync(dev);
  141. return ret_val;
  142. }
  143. static int als_set_power_state(struct i2c_client *client, bool on_off)
  144. {
  145. int ret_val;
  146. struct als_data *data = i2c_get_clientdata(client);
  147. mutex_lock(&data->mutex);
  148. ret_val = i2c_smbus_read_byte_data(client, 0x80);
  149. if (ret_val < 0)
  150. goto fail;
  151. if (on_off)
  152. ret_val = ret_val | 0x01;
  153. else
  154. ret_val = ret_val & 0xFE;
  155. ret_val = i2c_smbus_write_byte_data(client, 0x80, ret_val);
  156. fail:
  157. mutex_unlock(&data->mutex);
  158. return ret_val;
  159. }
  160. static DEVICE_ATTR(lux0_sensor_range, S_IRUGO | S_IWUSR,
  161. als_sensing_range_show, als_sensing_range_store);
  162. static DEVICE_ATTR(lux0_input, S_IRUGO, als_lux0_input_data_show, NULL);
  163. static struct attribute *mid_att_als[] = {
  164. &dev_attr_lux0_sensor_range.attr,
  165. &dev_attr_lux0_input.attr,
  166. NULL
  167. };
  168. static struct attribute_group m_als_gr = {
  169. .name = "apds9802als",
  170. .attrs = mid_att_als
  171. };
  172. static int als_set_default_config(struct i2c_client *client)
  173. {
  174. int ret_val;
  175. /* Write the command and then switch on */
  176. ret_val = i2c_smbus_write_byte_data(client, 0x80, 0x01);
  177. if (ret_val < 0) {
  178. dev_err(&client->dev, "failed default switch on write\n");
  179. return ret_val;
  180. }
  181. /* detection range: 1~64K Lux, maunal measurement */
  182. ret_val = i2c_smbus_write_byte_data(client, 0x81, 0x08);
  183. if (ret_val < 0)
  184. dev_err(&client->dev, "failed default LUX on write\n");
  185. /* We always get 0 for the 1st measurement after system power on,
  186. * so make sure it is finished before user asks for data.
  187. */
  188. als_wait_for_data_ready(&client->dev);
  189. return ret_val;
  190. }
  191. static int apds9802als_probe(struct i2c_client *client,
  192. const struct i2c_device_id *id)
  193. {
  194. int res;
  195. struct als_data *data;
  196. data = kzalloc(sizeof(struct als_data), GFP_KERNEL);
  197. if (data == NULL) {
  198. dev_err(&client->dev, "Memory allocation failed\n");
  199. return -ENOMEM;
  200. }
  201. i2c_set_clientdata(client, data);
  202. res = sysfs_create_group(&client->dev.kobj, &m_als_gr);
  203. if (res) {
  204. dev_err(&client->dev, "device create file failed\n");
  205. goto als_error1;
  206. }
  207. dev_info(&client->dev, "ALS chip found\n");
  208. als_set_default_config(client);
  209. mutex_init(&data->mutex);
  210. pm_runtime_set_active(&client->dev);
  211. pm_runtime_enable(&client->dev);
  212. return res;
  213. als_error1:
  214. kfree(data);
  215. return res;
  216. }
  217. static int apds9802als_remove(struct i2c_client *client)
  218. {
  219. struct als_data *data = i2c_get_clientdata(client);
  220. pm_runtime_get_sync(&client->dev);
  221. als_set_power_state(client, false);
  222. sysfs_remove_group(&client->dev.kobj, &m_als_gr);
  223. pm_runtime_disable(&client->dev);
  224. pm_runtime_set_suspended(&client->dev);
  225. pm_runtime_put_noidle(&client->dev);
  226. kfree(data);
  227. return 0;
  228. }
  229. #ifdef CONFIG_PM
  230. static int apds9802als_suspend(struct device *dev)
  231. {
  232. struct i2c_client *client = to_i2c_client(dev);
  233. als_set_power_state(client, false);
  234. return 0;
  235. }
  236. static int apds9802als_resume(struct device *dev)
  237. {
  238. struct i2c_client *client = to_i2c_client(dev);
  239. als_set_power_state(client, true);
  240. return 0;
  241. }
  242. static UNIVERSAL_DEV_PM_OPS(apds9802als_pm_ops, apds9802als_suspend,
  243. apds9802als_resume, NULL);
  244. #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
  245. #else /* CONFIG_PM */
  246. #define APDS9802ALS_PM_OPS NULL
  247. #endif /* CONFIG_PM */
  248. static struct i2c_device_id apds9802als_id[] = {
  249. { DRIVER_NAME, 0 },
  250. { }
  251. };
  252. MODULE_DEVICE_TABLE(i2c, apds9802als_id);
  253. static struct i2c_driver apds9802als_driver = {
  254. .driver = {
  255. .name = DRIVER_NAME,
  256. .pm = APDS9802ALS_PM_OPS,
  257. },
  258. .probe = apds9802als_probe,
  259. .remove = apds9802als_remove,
  260. .id_table = apds9802als_id,
  261. };
  262. module_i2c_driver(apds9802als_driver);
  263. MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
  264. MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
  265. MODULE_LICENSE("GPL v2");