apds9802als.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/err.h>
  28. #include <linux/delay.h>
  29. #include <linux/mutex.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/pm_runtime.h>
  32. #define ALS_MIN_RANGE_VAL 1
  33. #define ALS_MAX_RANGE_VAL 2
  34. #define POWER_STA_ENABLE 1
  35. #define POWER_STA_DISABLE 0
  36. #define DRIVER_NAME "apds9802als"
  37. struct als_data {
  38. struct mutex mutex;
  39. };
  40. static ssize_t als_sensing_range_show(struct device *dev,
  41. struct device_attribute *attr, char *buf)
  42. {
  43. struct i2c_client *client = to_i2c_client(dev);
  44. int val;
  45. val = i2c_smbus_read_byte_data(client, 0x81);
  46. if (val < 0)
  47. return val;
  48. if (val & 1)
  49. return sprintf(buf, "4095\n");
  50. else
  51. return sprintf(buf, "65535\n");
  52. }
  53. static int als_wait_for_data_ready(struct device *dev)
  54. {
  55. struct i2c_client *client = to_i2c_client(dev);
  56. int ret;
  57. int retry = 10;
  58. do {
  59. msleep(30);
  60. ret = i2c_smbus_read_byte_data(client, 0x86);
  61. } while (!(ret & 0x80) && retry--);
  62. if (!retry) {
  63. dev_warn(dev, "timeout waiting for data ready\n");
  64. return -ETIMEDOUT;
  65. }
  66. return 0;
  67. }
  68. static ssize_t als_lux0_input_data_show(struct device *dev,
  69. struct device_attribute *attr, char *buf)
  70. {
  71. struct i2c_client *client = to_i2c_client(dev);
  72. struct als_data *data = i2c_get_clientdata(client);
  73. int ret_val;
  74. int temp;
  75. /* Protect against parallel reads */
  76. pm_runtime_get_sync(dev);
  77. mutex_lock(&data->mutex);
  78. /* clear EOC interrupt status */
  79. i2c_smbus_write_byte(client, 0x40);
  80. /* start measurement */
  81. temp = i2c_smbus_read_byte_data(client, 0x81);
  82. i2c_smbus_write_byte_data(client, 0x81, temp | 0x08);
  83. ret_val = als_wait_for_data_ready(dev);
  84. if (ret_val < 0)
  85. goto failed;
  86. temp = i2c_smbus_read_byte_data(client, 0x8C); /* LSB data */
  87. if (temp < 0) {
  88. ret_val = temp;
  89. goto failed;
  90. }
  91. ret_val = i2c_smbus_read_byte_data(client, 0x8D); /* MSB data */
  92. if (ret_val < 0)
  93. goto failed;
  94. mutex_unlock(&data->mutex);
  95. pm_runtime_put_sync(dev);
  96. temp = (ret_val << 8) | temp;
  97. return sprintf(buf, "%d\n", temp);
  98. failed:
  99. mutex_unlock(&data->mutex);
  100. pm_runtime_put_sync(dev);
  101. return ret_val;
  102. }
  103. static ssize_t als_sensing_range_store(struct device *dev,
  104. struct device_attribute *attr, const char *buf, size_t count)
  105. {
  106. struct i2c_client *client = to_i2c_client(dev);
  107. struct als_data *data = i2c_get_clientdata(client);
  108. int ret_val;
  109. unsigned long val;
  110. if (strict_strtoul(buf, 10, &val))
  111. return -EINVAL;
  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 __devexit 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 i2c_client *client, pm_message_t mesg)
  231. {
  232. als_set_power_state(client, false);
  233. return 0;
  234. }
  235. static int apds9802als_resume(struct i2c_client *client)
  236. {
  237. als_set_default_config(client);
  238. return 0;
  239. }
  240. static int apds9802als_runtime_suspend(struct device *dev)
  241. {
  242. struct i2c_client *client = to_i2c_client(dev);
  243. als_set_power_state(client, false);
  244. return 0;
  245. }
  246. static int apds9802als_runtime_resume(struct device *dev)
  247. {
  248. struct i2c_client *client = to_i2c_client(dev);
  249. als_set_power_state(client, true);
  250. return 0;
  251. }
  252. static const struct dev_pm_ops apds9802als_pm_ops = {
  253. .runtime_suspend = apds9802als_runtime_suspend,
  254. .runtime_resume = apds9802als_runtime_resume,
  255. };
  256. #define APDS9802ALS_PM_OPS (&apds9802als_pm_ops)
  257. #else /* CONFIG_PM */
  258. #define apds9802als_suspend NULL
  259. #define apds9802als_resume NULL
  260. #define APDS9802ALS_PM_OPS NULL
  261. #endif /* CONFIG_PM */
  262. static struct i2c_device_id apds9802als_id[] = {
  263. { DRIVER_NAME, 0 },
  264. { }
  265. };
  266. MODULE_DEVICE_TABLE(i2c, apds9802als_id);
  267. static struct i2c_driver apds9802als_driver = {
  268. .driver = {
  269. .name = DRIVER_NAME,
  270. .pm = APDS9802ALS_PM_OPS,
  271. },
  272. .probe = apds9802als_probe,
  273. .remove = __devexit_p(apds9802als_remove),
  274. .suspend = apds9802als_suspend,
  275. .resume = apds9802als_resume,
  276. .id_table = apds9802als_id,
  277. };
  278. static int __init sensor_apds9802als_init(void)
  279. {
  280. return i2c_add_driver(&apds9802als_driver);
  281. }
  282. static void __exit sensor_apds9802als_exit(void)
  283. {
  284. i2c_del_driver(&apds9802als_driver);
  285. }
  286. module_init(sensor_apds9802als_init);
  287. module_exit(sensor_apds9802als_exit);
  288. MODULE_AUTHOR("Anantha Narayanan <Anantha.Narayanan@intel.com");
  289. MODULE_DESCRIPTION("Avago apds9802als ALS Driver");
  290. MODULE_LICENSE("GPL v2");