tsl2550.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * tsl2550.c - Linux kernel modules for ambient light sensor
  3. *
  4. * Copyright (C) 2007 Rodolfo Giometti <giometti@linux.it>
  5. * Copyright (C) 2007 Eurotech S.p.A. <info@eurotech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/mutex.h>
  26. #define TSL2550_DRV_NAME "tsl2550"
  27. #define DRIVER_VERSION "1.2"
  28. /*
  29. * Defines
  30. */
  31. #define TSL2550_POWER_DOWN 0x00
  32. #define TSL2550_POWER_UP 0x03
  33. #define TSL2550_STANDARD_RANGE 0x18
  34. #define TSL2550_EXTENDED_RANGE 0x1d
  35. #define TSL2550_READ_ADC0 0x43
  36. #define TSL2550_READ_ADC1 0x83
  37. /*
  38. * Structs
  39. */
  40. struct tsl2550_data {
  41. struct i2c_client *client;
  42. struct mutex update_lock;
  43. unsigned int power_state:1;
  44. unsigned int operating_mode:1;
  45. };
  46. /*
  47. * Global data
  48. */
  49. static const u8 TSL2550_MODE_RANGE[2] = {
  50. TSL2550_STANDARD_RANGE, TSL2550_EXTENDED_RANGE,
  51. };
  52. /*
  53. * Management functions
  54. */
  55. static int tsl2550_set_operating_mode(struct i2c_client *client, int mode)
  56. {
  57. struct tsl2550_data *data = i2c_get_clientdata(client);
  58. int ret = i2c_smbus_write_byte(client, TSL2550_MODE_RANGE[mode]);
  59. data->operating_mode = mode;
  60. return ret;
  61. }
  62. static int tsl2550_set_power_state(struct i2c_client *client, int state)
  63. {
  64. struct tsl2550_data *data = i2c_get_clientdata(client);
  65. int ret;
  66. if (state == 0)
  67. ret = i2c_smbus_write_byte(client, TSL2550_POWER_DOWN);
  68. else {
  69. ret = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
  70. /* On power up we should reset operating mode also... */
  71. tsl2550_set_operating_mode(client, data->operating_mode);
  72. }
  73. data->power_state = state;
  74. return ret;
  75. }
  76. static int tsl2550_get_adc_value(struct i2c_client *client, u8 cmd)
  77. {
  78. int ret;
  79. ret = i2c_smbus_read_byte_data(client, cmd);
  80. if (ret < 0)
  81. return ret;
  82. if (!(ret & 0x80))
  83. return -EAGAIN;
  84. return ret & 0x7f; /* remove the "valid" bit */
  85. }
  86. /*
  87. * LUX calculation
  88. */
  89. #define TSL2550_MAX_LUX 1846
  90. static const u8 ratio_lut[] = {
  91. 100, 100, 100, 100, 100, 100, 100, 100,
  92. 100, 100, 100, 100, 100, 100, 99, 99,
  93. 99, 99, 99, 99, 99, 99, 99, 99,
  94. 99, 99, 99, 98, 98, 98, 98, 98,
  95. 98, 98, 97, 97, 97, 97, 97, 96,
  96. 96, 96, 96, 95, 95, 95, 94, 94,
  97. 93, 93, 93, 92, 92, 91, 91, 90,
  98. 89, 89, 88, 87, 87, 86, 85, 84,
  99. 83, 82, 81, 80, 79, 78, 77, 75,
  100. 74, 73, 71, 69, 68, 66, 64, 62,
  101. 60, 58, 56, 54, 52, 49, 47, 44,
  102. 42, 41, 40, 40, 39, 39, 38, 38,
  103. 37, 37, 37, 36, 36, 36, 35, 35,
  104. 35, 35, 34, 34, 34, 34, 33, 33,
  105. 33, 33, 32, 32, 32, 32, 32, 31,
  106. 31, 31, 31, 31, 30, 30, 30, 30,
  107. 30,
  108. };
  109. static const u16 count_lut[] = {
  110. 0, 1, 2, 3, 4, 5, 6, 7,
  111. 8, 9, 10, 11, 12, 13, 14, 15,
  112. 16, 18, 20, 22, 24, 26, 28, 30,
  113. 32, 34, 36, 38, 40, 42, 44, 46,
  114. 49, 53, 57, 61, 65, 69, 73, 77,
  115. 81, 85, 89, 93, 97, 101, 105, 109,
  116. 115, 123, 131, 139, 147, 155, 163, 171,
  117. 179, 187, 195, 203, 211, 219, 227, 235,
  118. 247, 263, 279, 295, 311, 327, 343, 359,
  119. 375, 391, 407, 423, 439, 455, 471, 487,
  120. 511, 543, 575, 607, 639, 671, 703, 735,
  121. 767, 799, 831, 863, 895, 927, 959, 991,
  122. 1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
  123. 1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
  124. 2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
  125. 3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015,
  126. };
  127. /*
  128. * This function is described into Taos TSL2550 Designer's Notebook
  129. * pages 2, 3.
  130. */
  131. static int tsl2550_calculate_lux(u8 ch0, u8 ch1)
  132. {
  133. unsigned int lux;
  134. /* Look up count from channel values */
  135. u16 c0 = count_lut[ch0];
  136. u16 c1 = count_lut[ch1];
  137. /*
  138. * Calculate ratio.
  139. * Note: the "128" is a scaling factor
  140. */
  141. u8 r = 128;
  142. /* Avoid division by 0 and count 1 cannot be greater than count 0 */
  143. if (c1 <= c0)
  144. if (c0) {
  145. r = c1 * 128 / c0;
  146. /* Calculate LUX */
  147. lux = ((c0 - c1) * ratio_lut[r]) / 256;
  148. } else
  149. lux = 0;
  150. else
  151. return -EAGAIN;
  152. /* LUX range check */
  153. return lux > TSL2550_MAX_LUX ? TSL2550_MAX_LUX : lux;
  154. }
  155. /*
  156. * SysFS support
  157. */
  158. static ssize_t tsl2550_show_power_state(struct device *dev,
  159. struct device_attribute *attr, char *buf)
  160. {
  161. struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
  162. return sprintf(buf, "%u\n", data->power_state);
  163. }
  164. static ssize_t tsl2550_store_power_state(struct device *dev,
  165. struct device_attribute *attr, const char *buf, size_t count)
  166. {
  167. struct i2c_client *client = to_i2c_client(dev);
  168. struct tsl2550_data *data = i2c_get_clientdata(client);
  169. unsigned long val = simple_strtoul(buf, NULL, 10);
  170. int ret;
  171. if (val < 0 || val > 1)
  172. return -EINVAL;
  173. mutex_lock(&data->update_lock);
  174. ret = tsl2550_set_power_state(client, val);
  175. mutex_unlock(&data->update_lock);
  176. if (ret < 0)
  177. return ret;
  178. return count;
  179. }
  180. static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
  181. tsl2550_show_power_state, tsl2550_store_power_state);
  182. static ssize_t tsl2550_show_operating_mode(struct device *dev,
  183. struct device_attribute *attr, char *buf)
  184. {
  185. struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
  186. return sprintf(buf, "%u\n", data->operating_mode);
  187. }
  188. static ssize_t tsl2550_store_operating_mode(struct device *dev,
  189. struct device_attribute *attr, const char *buf, size_t count)
  190. {
  191. struct i2c_client *client = to_i2c_client(dev);
  192. struct tsl2550_data *data = i2c_get_clientdata(client);
  193. unsigned long val = simple_strtoul(buf, NULL, 10);
  194. int ret;
  195. if (val < 0 || val > 1)
  196. return -EINVAL;
  197. if (data->power_state == 0)
  198. return -EBUSY;
  199. mutex_lock(&data->update_lock);
  200. ret = tsl2550_set_operating_mode(client, val);
  201. mutex_unlock(&data->update_lock);
  202. if (ret < 0)
  203. return ret;
  204. return count;
  205. }
  206. static DEVICE_ATTR(operating_mode, S_IWUSR | S_IRUGO,
  207. tsl2550_show_operating_mode, tsl2550_store_operating_mode);
  208. static ssize_t __tsl2550_show_lux(struct i2c_client *client, char *buf)
  209. {
  210. struct tsl2550_data *data = i2c_get_clientdata(client);
  211. u8 ch0, ch1;
  212. int ret;
  213. ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC0);
  214. if (ret < 0)
  215. return ret;
  216. ch0 = ret;
  217. ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC1);
  218. if (ret < 0)
  219. return ret;
  220. ch1 = ret;
  221. /* Do the job */
  222. ret = tsl2550_calculate_lux(ch0, ch1);
  223. if (ret < 0)
  224. return ret;
  225. if (data->operating_mode == 1)
  226. ret *= 5;
  227. return sprintf(buf, "%d\n", ret);
  228. }
  229. static ssize_t tsl2550_show_lux1_input(struct device *dev,
  230. struct device_attribute *attr, char *buf)
  231. {
  232. struct i2c_client *client = to_i2c_client(dev);
  233. struct tsl2550_data *data = i2c_get_clientdata(client);
  234. int ret;
  235. /* No LUX data if not operational */
  236. if (!data->power_state)
  237. return -EBUSY;
  238. mutex_lock(&data->update_lock);
  239. ret = __tsl2550_show_lux(client, buf);
  240. mutex_unlock(&data->update_lock);
  241. return ret;
  242. }
  243. static DEVICE_ATTR(lux1_input, S_IRUGO,
  244. tsl2550_show_lux1_input, NULL);
  245. static struct attribute *tsl2550_attributes[] = {
  246. &dev_attr_power_state.attr,
  247. &dev_attr_operating_mode.attr,
  248. &dev_attr_lux1_input.attr,
  249. NULL
  250. };
  251. static const struct attribute_group tsl2550_attr_group = {
  252. .attrs = tsl2550_attributes,
  253. };
  254. /*
  255. * Initialization function
  256. */
  257. static int tsl2550_init_client(struct i2c_client *client)
  258. {
  259. struct tsl2550_data *data = i2c_get_clientdata(client);
  260. int err;
  261. /*
  262. * Probe the chip. To do so we try to power up the device and then to
  263. * read back the 0x03 code
  264. */
  265. err = i2c_smbus_read_byte_data(client, TSL2550_POWER_UP);
  266. if (err < 0)
  267. return err;
  268. if (err != TSL2550_POWER_UP)
  269. return -ENODEV;
  270. data->power_state = 1;
  271. /* Set the default operating mode */
  272. err = i2c_smbus_write_byte(client,
  273. TSL2550_MODE_RANGE[data->operating_mode]);
  274. if (err < 0)
  275. return err;
  276. return 0;
  277. }
  278. /*
  279. * I2C init/probing/exit functions
  280. */
  281. static struct i2c_driver tsl2550_driver;
  282. static int __devinit tsl2550_probe(struct i2c_client *client,
  283. const struct i2c_device_id *id)
  284. {
  285. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  286. struct tsl2550_data *data;
  287. int *opmode, err = 0;
  288. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE
  289. | I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  290. err = -EIO;
  291. goto exit;
  292. }
  293. data = kzalloc(sizeof(struct tsl2550_data), GFP_KERNEL);
  294. if (!data) {
  295. err = -ENOMEM;
  296. goto exit;
  297. }
  298. data->client = client;
  299. i2c_set_clientdata(client, data);
  300. /* Check platform data */
  301. opmode = client->dev.platform_data;
  302. if (opmode) {
  303. if (*opmode < 0 || *opmode > 1) {
  304. dev_err(&client->dev, "invalid operating_mode (%d)\n",
  305. *opmode);
  306. err = -EINVAL;
  307. goto exit_kfree;
  308. }
  309. data->operating_mode = *opmode;
  310. } else
  311. data->operating_mode = 0; /* default mode is standard */
  312. dev_info(&client->dev, "%s operating mode\n",
  313. data->operating_mode ? "extended" : "standard");
  314. mutex_init(&data->update_lock);
  315. /* Initialize the TSL2550 chip */
  316. err = tsl2550_init_client(client);
  317. if (err)
  318. goto exit_kfree;
  319. /* Register sysfs hooks */
  320. err = sysfs_create_group(&client->dev.kobj, &tsl2550_attr_group);
  321. if (err)
  322. goto exit_kfree;
  323. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  324. return 0;
  325. exit_kfree:
  326. kfree(data);
  327. exit:
  328. return err;
  329. }
  330. static int __devexit tsl2550_remove(struct i2c_client *client)
  331. {
  332. sysfs_remove_group(&client->dev.kobj, &tsl2550_attr_group);
  333. /* Power down the device */
  334. tsl2550_set_power_state(client, 0);
  335. kfree(i2c_get_clientdata(client));
  336. return 0;
  337. }
  338. #ifdef CONFIG_PM
  339. static int tsl2550_suspend(struct i2c_client *client, pm_message_t mesg)
  340. {
  341. return tsl2550_set_power_state(client, 0);
  342. }
  343. static int tsl2550_resume(struct i2c_client *client)
  344. {
  345. return tsl2550_set_power_state(client, 1);
  346. }
  347. #else
  348. #define tsl2550_suspend NULL
  349. #define tsl2550_resume NULL
  350. #endif /* CONFIG_PM */
  351. static const struct i2c_device_id tsl2550_id[] = {
  352. { "tsl2550", 0 },
  353. { }
  354. };
  355. MODULE_DEVICE_TABLE(i2c, tsl2550_id);
  356. static struct i2c_driver tsl2550_driver = {
  357. .driver = {
  358. .name = TSL2550_DRV_NAME,
  359. .owner = THIS_MODULE,
  360. },
  361. .suspend = tsl2550_suspend,
  362. .resume = tsl2550_resume,
  363. .probe = tsl2550_probe,
  364. .remove = __devexit_p(tsl2550_remove),
  365. .id_table = tsl2550_id,
  366. };
  367. static int __init tsl2550_init(void)
  368. {
  369. return i2c_add_driver(&tsl2550_driver);
  370. }
  371. static void __exit tsl2550_exit(void)
  372. {
  373. i2c_del_driver(&tsl2550_driver);
  374. }
  375. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  376. MODULE_DESCRIPTION("TSL2550 ambient light sensor driver");
  377. MODULE_LICENSE("GPL");
  378. MODULE_VERSION(DRIVER_VERSION);
  379. module_init(tsl2550_init);
  380. module_exit(tsl2550_exit);