isl29003.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * isl29003.c - Linux kernel module for
  3. * Intersil ISL29003 ambient light sensor
  4. *
  5. * See file:Documentation/misc-devices/isl29003
  6. *
  7. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  8. *
  9. * Based on code written by
  10. * Rodolfo Giometti <giometti@linux.it>
  11. * Eurotech S.p.A. <info@eurotech.it>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/i2c.h>
  31. #include <linux/mutex.h>
  32. #include <linux/delay.h>
  33. #define ISL29003_DRV_NAME "isl29003"
  34. #define DRIVER_VERSION "1.0"
  35. #define ISL29003_REG_COMMAND 0x00
  36. #define ISL29003_ADC_ENABLED (1 << 7)
  37. #define ISL29003_ADC_PD (1 << 6)
  38. #define ISL29003_TIMING_INT (1 << 5)
  39. #define ISL29003_MODE_SHIFT (2)
  40. #define ISL29003_MODE_MASK (0x3 << ISL29003_MODE_SHIFT)
  41. #define ISL29003_RES_SHIFT (0)
  42. #define ISL29003_RES_MASK (0x3 << ISL29003_RES_SHIFT)
  43. #define ISL29003_REG_CONTROL 0x01
  44. #define ISL29003_INT_FLG (1 << 5)
  45. #define ISL29003_RANGE_SHIFT (2)
  46. #define ISL29003_RANGE_MASK (0x3 << ISL29003_RANGE_SHIFT)
  47. #define ISL29003_INT_PERSISTS_SHIFT (0)
  48. #define ISL29003_INT_PERSISTS_MASK (0xf << ISL29003_INT_PERSISTS_SHIFT)
  49. #define ISL29003_REG_IRQ_THRESH_HI 0x02
  50. #define ISL29003_REG_IRQ_THRESH_LO 0x03
  51. #define ISL29003_REG_LSB_SENSOR 0x04
  52. #define ISL29003_REG_MSB_SENSOR 0x05
  53. #define ISL29003_REG_LSB_TIMER 0x06
  54. #define ISL29003_REG_MSB_TIMER 0x07
  55. #define ISL29003_NUM_CACHABLE_REGS 4
  56. struct isl29003_data {
  57. struct i2c_client *client;
  58. struct mutex lock;
  59. u8 reg_cache[ISL29003_NUM_CACHABLE_REGS];
  60. u8 power_state_before_suspend;
  61. };
  62. static int gain_range[] = {
  63. 1000, 4000, 16000, 64000
  64. };
  65. /*
  66. * register access helpers
  67. */
  68. static int __isl29003_read_reg(struct i2c_client *client,
  69. u32 reg, u8 mask, u8 shift)
  70. {
  71. struct isl29003_data *data = i2c_get_clientdata(client);
  72. return (data->reg_cache[reg] & mask) >> shift;
  73. }
  74. static int __isl29003_write_reg(struct i2c_client *client,
  75. u32 reg, u8 mask, u8 shift, u8 val)
  76. {
  77. struct isl29003_data *data = i2c_get_clientdata(client);
  78. int ret = 0;
  79. u8 tmp;
  80. if (reg >= ISL29003_NUM_CACHABLE_REGS)
  81. return -EINVAL;
  82. mutex_lock(&data->lock);
  83. tmp = data->reg_cache[reg];
  84. tmp &= ~mask;
  85. tmp |= val << shift;
  86. ret = i2c_smbus_write_byte_data(client, reg, tmp);
  87. if (!ret)
  88. data->reg_cache[reg] = tmp;
  89. mutex_unlock(&data->lock);
  90. return ret;
  91. }
  92. /*
  93. * internally used functions
  94. */
  95. /* range */
  96. static int isl29003_get_range(struct i2c_client *client)
  97. {
  98. return __isl29003_read_reg(client, ISL29003_REG_CONTROL,
  99. ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT);
  100. }
  101. static int isl29003_set_range(struct i2c_client *client, int range)
  102. {
  103. return __isl29003_write_reg(client, ISL29003_REG_CONTROL,
  104. ISL29003_RANGE_MASK, ISL29003_RANGE_SHIFT, range);
  105. }
  106. /* resolution */
  107. static int isl29003_get_resolution(struct i2c_client *client)
  108. {
  109. return __isl29003_read_reg(client, ISL29003_REG_COMMAND,
  110. ISL29003_RES_MASK, ISL29003_RES_SHIFT);
  111. }
  112. static int isl29003_set_resolution(struct i2c_client *client, int res)
  113. {
  114. return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
  115. ISL29003_RES_MASK, ISL29003_RES_SHIFT, res);
  116. }
  117. /* mode */
  118. static int isl29003_get_mode(struct i2c_client *client)
  119. {
  120. return __isl29003_read_reg(client, ISL29003_REG_COMMAND,
  121. ISL29003_RES_MASK, ISL29003_RES_SHIFT);
  122. }
  123. static int isl29003_set_mode(struct i2c_client *client, int mode)
  124. {
  125. return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
  126. ISL29003_RES_MASK, ISL29003_RES_SHIFT, mode);
  127. }
  128. /* power_state */
  129. static int isl29003_set_power_state(struct i2c_client *client, int state)
  130. {
  131. return __isl29003_write_reg(client, ISL29003_REG_COMMAND,
  132. ISL29003_ADC_ENABLED | ISL29003_ADC_PD, 0,
  133. state ? ISL29003_ADC_ENABLED : ISL29003_ADC_PD);
  134. }
  135. static int isl29003_get_power_state(struct i2c_client *client)
  136. {
  137. struct isl29003_data *data = i2c_get_clientdata(client);
  138. u8 cmdreg = data->reg_cache[ISL29003_REG_COMMAND];
  139. return ~cmdreg & ISL29003_ADC_PD;
  140. }
  141. static int isl29003_get_adc_value(struct i2c_client *client)
  142. {
  143. struct isl29003_data *data = i2c_get_clientdata(client);
  144. int lsb, msb, range, bitdepth;
  145. mutex_lock(&data->lock);
  146. lsb = i2c_smbus_read_byte_data(client, ISL29003_REG_LSB_SENSOR);
  147. if (lsb < 0) {
  148. mutex_unlock(&data->lock);
  149. return lsb;
  150. }
  151. msb = i2c_smbus_read_byte_data(client, ISL29003_REG_MSB_SENSOR);
  152. mutex_unlock(&data->lock);
  153. if (msb < 0)
  154. return msb;
  155. range = isl29003_get_range(client);
  156. bitdepth = (4 - isl29003_get_resolution(client)) * 4;
  157. return (((msb << 8) | lsb) * gain_range[range]) >> bitdepth;
  158. }
  159. /*
  160. * sysfs layer
  161. */
  162. /* range */
  163. static ssize_t isl29003_show_range(struct device *dev,
  164. struct device_attribute *attr, char *buf)
  165. {
  166. struct i2c_client *client = to_i2c_client(dev);
  167. return sprintf(buf, "%i\n", isl29003_get_range(client));
  168. }
  169. static ssize_t isl29003_store_range(struct device *dev,
  170. struct device_attribute *attr,
  171. const char *buf, size_t count)
  172. {
  173. struct i2c_client *client = to_i2c_client(dev);
  174. unsigned long val;
  175. int ret;
  176. if ((strict_strtoul(buf, 10, &val) < 0) || (val > 3))
  177. return -EINVAL;
  178. ret = isl29003_set_range(client, val);
  179. if (ret < 0)
  180. return ret;
  181. return count;
  182. }
  183. static DEVICE_ATTR(range, S_IWUSR | S_IRUGO,
  184. isl29003_show_range, isl29003_store_range);
  185. /* resolution */
  186. static ssize_t isl29003_show_resolution(struct device *dev,
  187. struct device_attribute *attr,
  188. char *buf)
  189. {
  190. struct i2c_client *client = to_i2c_client(dev);
  191. return sprintf(buf, "%d\n", isl29003_get_resolution(client));
  192. }
  193. static ssize_t isl29003_store_resolution(struct device *dev,
  194. struct device_attribute *attr,
  195. const char *buf, size_t count)
  196. {
  197. struct i2c_client *client = to_i2c_client(dev);
  198. unsigned long val;
  199. int ret;
  200. if ((strict_strtoul(buf, 10, &val) < 0) || (val > 3))
  201. return -EINVAL;
  202. ret = isl29003_set_resolution(client, val);
  203. if (ret < 0)
  204. return ret;
  205. return count;
  206. }
  207. static DEVICE_ATTR(resolution, S_IWUSR | S_IRUGO,
  208. isl29003_show_resolution, isl29003_store_resolution);
  209. /* mode */
  210. static ssize_t isl29003_show_mode(struct device *dev,
  211. struct device_attribute *attr, char *buf)
  212. {
  213. struct i2c_client *client = to_i2c_client(dev);
  214. return sprintf(buf, "%d\n", isl29003_get_mode(client));
  215. }
  216. static ssize_t isl29003_store_mode(struct device *dev,
  217. struct device_attribute *attr, const char *buf, size_t count)
  218. {
  219. struct i2c_client *client = to_i2c_client(dev);
  220. unsigned long val;
  221. int ret;
  222. if ((strict_strtoul(buf, 10, &val) < 0) || (val > 2))
  223. return -EINVAL;
  224. ret = isl29003_set_mode(client, val);
  225. if (ret < 0)
  226. return ret;
  227. return count;
  228. }
  229. static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
  230. isl29003_show_mode, isl29003_store_mode);
  231. /* power state */
  232. static ssize_t isl29003_show_power_state(struct device *dev,
  233. struct device_attribute *attr,
  234. char *buf)
  235. {
  236. struct i2c_client *client = to_i2c_client(dev);
  237. return sprintf(buf, "%d\n", isl29003_get_power_state(client));
  238. }
  239. static ssize_t isl29003_store_power_state(struct device *dev,
  240. struct device_attribute *attr,
  241. const char *buf, size_t count)
  242. {
  243. struct i2c_client *client = to_i2c_client(dev);
  244. unsigned long val;
  245. int ret;
  246. if ((strict_strtoul(buf, 10, &val) < 0) || (val > 1))
  247. return -EINVAL;
  248. ret = isl29003_set_power_state(client, val);
  249. return ret ? ret : count;
  250. }
  251. static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
  252. isl29003_show_power_state, isl29003_store_power_state);
  253. /* lux */
  254. static ssize_t isl29003_show_lux(struct device *dev,
  255. struct device_attribute *attr, char *buf)
  256. {
  257. struct i2c_client *client = to_i2c_client(dev);
  258. /* No LUX data if not operational */
  259. if (!isl29003_get_power_state(client))
  260. return -EBUSY;
  261. return sprintf(buf, "%d\n", isl29003_get_adc_value(client));
  262. }
  263. static DEVICE_ATTR(lux, S_IRUGO, isl29003_show_lux, NULL);
  264. static struct attribute *isl29003_attributes[] = {
  265. &dev_attr_range.attr,
  266. &dev_attr_resolution.attr,
  267. &dev_attr_mode.attr,
  268. &dev_attr_power_state.attr,
  269. &dev_attr_lux.attr,
  270. NULL
  271. };
  272. static const struct attribute_group isl29003_attr_group = {
  273. .attrs = isl29003_attributes,
  274. };
  275. static int isl29003_init_client(struct i2c_client *client)
  276. {
  277. struct isl29003_data *data = i2c_get_clientdata(client);
  278. int i;
  279. /* read all the registers once to fill the cache.
  280. * if one of the reads fails, we consider the init failed */
  281. for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++) {
  282. int v = i2c_smbus_read_byte_data(client, i);
  283. if (v < 0)
  284. return -ENODEV;
  285. data->reg_cache[i] = v;
  286. }
  287. /* set defaults */
  288. isl29003_set_range(client, 0);
  289. isl29003_set_resolution(client, 0);
  290. isl29003_set_mode(client, 0);
  291. isl29003_set_power_state(client, 0);
  292. return 0;
  293. }
  294. /*
  295. * I2C layer
  296. */
  297. static int __devinit isl29003_probe(struct i2c_client *client,
  298. const struct i2c_device_id *id)
  299. {
  300. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  301. struct isl29003_data *data;
  302. int err = 0;
  303. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  304. return -EIO;
  305. data = kzalloc(sizeof(struct isl29003_data), GFP_KERNEL);
  306. if (!data)
  307. return -ENOMEM;
  308. data->client = client;
  309. i2c_set_clientdata(client, data);
  310. mutex_init(&data->lock);
  311. /* initialize the ISL29003 chip */
  312. err = isl29003_init_client(client);
  313. if (err)
  314. goto exit_kfree;
  315. /* register sysfs hooks */
  316. err = sysfs_create_group(&client->dev.kobj, &isl29003_attr_group);
  317. if (err)
  318. goto exit_kfree;
  319. dev_info(&client->dev, "driver version %s enabled\n", DRIVER_VERSION);
  320. return 0;
  321. exit_kfree:
  322. kfree(data);
  323. return err;
  324. }
  325. static int __devexit isl29003_remove(struct i2c_client *client)
  326. {
  327. sysfs_remove_group(&client->dev.kobj, &isl29003_attr_group);
  328. isl29003_set_power_state(client, 0);
  329. kfree(i2c_get_clientdata(client));
  330. return 0;
  331. }
  332. #ifdef CONFIG_PM
  333. static int isl29003_suspend(struct i2c_client *client, pm_message_t mesg)
  334. {
  335. struct isl29003_data *data = i2c_get_clientdata(client);
  336. data->power_state_before_suspend = isl29003_get_power_state(client);
  337. return isl29003_set_power_state(client, 0);
  338. }
  339. static int isl29003_resume(struct i2c_client *client)
  340. {
  341. int i;
  342. struct isl29003_data *data = i2c_get_clientdata(client);
  343. /* restore registers from cache */
  344. for (i = 0; i < ARRAY_SIZE(data->reg_cache); i++)
  345. if (i2c_smbus_write_byte_data(client, i, data->reg_cache[i]))
  346. return -EIO;
  347. return isl29003_set_power_state(client,
  348. data->power_state_before_suspend);
  349. }
  350. #else
  351. #define isl29003_suspend NULL
  352. #define isl29003_resume NULL
  353. #endif /* CONFIG_PM */
  354. static const struct i2c_device_id isl29003_id[] = {
  355. { "isl29003", 0 },
  356. {}
  357. };
  358. MODULE_DEVICE_TABLE(i2c, isl29003_id);
  359. static struct i2c_driver isl29003_driver = {
  360. .driver = {
  361. .name = ISL29003_DRV_NAME,
  362. .owner = THIS_MODULE,
  363. },
  364. .suspend = isl29003_suspend,
  365. .resume = isl29003_resume,
  366. .probe = isl29003_probe,
  367. .remove = __devexit_p(isl29003_remove),
  368. .id_table = isl29003_id,
  369. };
  370. static int __init isl29003_init(void)
  371. {
  372. return i2c_add_driver(&isl29003_driver);
  373. }
  374. static void __exit isl29003_exit(void)
  375. {
  376. i2c_del_driver(&isl29003_driver);
  377. }
  378. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  379. MODULE_DESCRIPTION("ISL29003 ambient light sensor driver");
  380. MODULE_LICENSE("GPL v2");
  381. MODULE_VERSION(DRIVER_VERSION);
  382. module_init(isl29003_init);
  383. module_exit(isl29003_exit);