tsl2550.c 11 KB

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