tsl2550.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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.2"
  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 (c1 <= c0)
  162. if (c0) {
  163. r = c1 * 128 / c0;
  164. /* Calculate LUX */
  165. lux = ((c0 - c1) * ratio_lut[r]) / 256;
  166. } else
  167. lux = 0;
  168. else
  169. return -EAGAIN;
  170. /* LUX range check */
  171. return lux > TSL2550_MAX_LUX ? TSL2550_MAX_LUX : lux;
  172. }
  173. /*
  174. * SysFS support
  175. */
  176. static ssize_t tsl2550_show_power_state(struct device *dev,
  177. struct device_attribute *attr, char *buf)
  178. {
  179. struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
  180. return sprintf(buf, "%u\n", data->power_state);
  181. }
  182. static ssize_t tsl2550_store_power_state(struct device *dev,
  183. struct device_attribute *attr, const char *buf, size_t count)
  184. {
  185. struct i2c_client *client = to_i2c_client(dev);
  186. struct tsl2550_data *data = i2c_get_clientdata(client);
  187. unsigned long val = simple_strtoul(buf, NULL, 10);
  188. int ret;
  189. if (val < 0 || val > 1)
  190. return -EINVAL;
  191. mutex_lock(&data->update_lock);
  192. ret = tsl2550_set_power_state(client, val);
  193. mutex_unlock(&data->update_lock);
  194. if (ret < 0)
  195. return ret;
  196. return count;
  197. }
  198. static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
  199. tsl2550_show_power_state, tsl2550_store_power_state);
  200. static ssize_t tsl2550_show_operating_mode(struct device *dev,
  201. struct device_attribute *attr, char *buf)
  202. {
  203. struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
  204. return sprintf(buf, "%u\n", data->operating_mode);
  205. }
  206. static ssize_t tsl2550_store_operating_mode(struct device *dev,
  207. struct device_attribute *attr, const char *buf, size_t count)
  208. {
  209. struct i2c_client *client = to_i2c_client(dev);
  210. struct tsl2550_data *data = i2c_get_clientdata(client);
  211. unsigned long val = simple_strtoul(buf, NULL, 10);
  212. int ret;
  213. if (val < 0 || val > 1)
  214. return -EINVAL;
  215. if (data->power_state == 0)
  216. return -EBUSY;
  217. mutex_lock(&data->update_lock);
  218. ret = tsl2550_set_operating_mode(client, val);
  219. mutex_unlock(&data->update_lock);
  220. if (ret < 0)
  221. return ret;
  222. return count;
  223. }
  224. static DEVICE_ATTR(operating_mode, S_IWUSR | S_IRUGO,
  225. tsl2550_show_operating_mode, tsl2550_store_operating_mode);
  226. static ssize_t __tsl2550_show_lux(struct i2c_client *client, char *buf)
  227. {
  228. u8 ch0, ch1;
  229. int ret;
  230. ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC0);
  231. if (ret < 0)
  232. return ret;
  233. ch0 = ret;
  234. mdelay(1);
  235. ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC1);
  236. if (ret < 0)
  237. return ret;
  238. ch1 = ret;
  239. /* Do the job */
  240. ret = tsl2550_calculate_lux(ch0, ch1);
  241. if (ret < 0)
  242. return ret;
  243. return sprintf(buf, "%d\n", ret);
  244. }
  245. static ssize_t tsl2550_show_lux1_input(struct device *dev,
  246. struct device_attribute *attr, char *buf)
  247. {
  248. struct i2c_client *client = to_i2c_client(dev);
  249. struct tsl2550_data *data = i2c_get_clientdata(client);
  250. int ret;
  251. /* No LUX data if not operational */
  252. if (!data->power_state)
  253. return -EBUSY;
  254. mutex_lock(&data->update_lock);
  255. ret = __tsl2550_show_lux(client, buf);
  256. mutex_unlock(&data->update_lock);
  257. return ret;
  258. }
  259. static DEVICE_ATTR(lux1_input, S_IRUGO,
  260. tsl2550_show_lux1_input, NULL);
  261. static struct attribute *tsl2550_attributes[] = {
  262. &dev_attr_power_state.attr,
  263. &dev_attr_operating_mode.attr,
  264. &dev_attr_lux1_input.attr,
  265. NULL
  266. };
  267. static const struct attribute_group tsl2550_attr_group = {
  268. .attrs = tsl2550_attributes,
  269. };
  270. /*
  271. * Initialization function
  272. */
  273. static int tsl2550_init_client(struct i2c_client *client)
  274. {
  275. struct tsl2550_data *data = i2c_get_clientdata(client);
  276. int err;
  277. /*
  278. * Probe the chip. To do so we try to power up the device and then to
  279. * read back the 0x03 code
  280. */
  281. err = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
  282. if (err < 0)
  283. return err;
  284. mdelay(1);
  285. if (i2c_smbus_read_byte(client) != TSL2550_POWER_UP)
  286. return -ENODEV;
  287. data->power_state = 1;
  288. /* Set the default operating mode */
  289. err = i2c_smbus_write_byte(client,
  290. TSL2550_MODE_RANGE[data->operating_mode]);
  291. if (err < 0)
  292. return err;
  293. return 0;
  294. }
  295. /*
  296. * I2C init/probing/exit functions
  297. */
  298. static struct i2c_driver tsl2550_driver;
  299. static int __devinit tsl2550_probe(struct i2c_client *client,
  300. const struct i2c_device_id *id)
  301. {
  302. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  303. struct tsl2550_data *data;
  304. int *opmode, err = 0;
  305. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) {
  306. err = -EIO;
  307. goto exit;
  308. }
  309. data = kzalloc(sizeof(struct tsl2550_data), GFP_KERNEL);
  310. if (!data) {
  311. err = -ENOMEM;
  312. goto exit;
  313. }
  314. data->client = client;
  315. i2c_set_clientdata(client, data);
  316. /* Check platform data */
  317. opmode = client->dev.platform_data;
  318. if (opmode) {
  319. if (*opmode < 0 || *opmode > 1) {
  320. dev_err(&client->dev, "invalid operating_mode (%d)\n",
  321. *opmode);
  322. err = -EINVAL;
  323. goto exit_kfree;
  324. }
  325. data->operating_mode = *opmode;
  326. } else
  327. data->operating_mode = 0; /* default mode is standard */
  328. dev_info(&client->dev, "%s operating mode\n",
  329. data->operating_mode ? "extended" : "standard");
  330. mutex_init(&data->update_lock);
  331. /* Initialize the TSL2550 chip */
  332. err = tsl2550_init_client(client);
  333. if (err)
  334. goto exit_kfree;
  335. /* Register sysfs hooks */
  336. err = sysfs_create_group(&client->dev.kobj, &tsl2550_attr_group);
  337. if (err)
  338. goto exit_kfree;
  339. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  340. return 0;
  341. exit_kfree:
  342. kfree(data);
  343. exit:
  344. return err;
  345. }
  346. static int __devexit tsl2550_remove(struct i2c_client *client)
  347. {
  348. sysfs_remove_group(&client->dev.kobj, &tsl2550_attr_group);
  349. /* Power down the device */
  350. tsl2550_set_power_state(client, 0);
  351. kfree(i2c_get_clientdata(client));
  352. return 0;
  353. }
  354. #ifdef CONFIG_PM
  355. static int tsl2550_suspend(struct i2c_client *client, pm_message_t mesg)
  356. {
  357. return tsl2550_set_power_state(client, 0);
  358. }
  359. static int tsl2550_resume(struct i2c_client *client)
  360. {
  361. return tsl2550_set_power_state(client, 1);
  362. }
  363. #else
  364. #define tsl2550_suspend NULL
  365. #define tsl2550_resume NULL
  366. #endif /* CONFIG_PM */
  367. static const struct i2c_device_id tsl2550_id[] = {
  368. { "tsl2550", 0 },
  369. { }
  370. };
  371. MODULE_DEVICE_TABLE(i2c, tsl2550_id);
  372. static struct i2c_driver tsl2550_driver = {
  373. .driver = {
  374. .name = TSL2550_DRV_NAME,
  375. .owner = THIS_MODULE,
  376. },
  377. .suspend = tsl2550_suspend,
  378. .resume = tsl2550_resume,
  379. .probe = tsl2550_probe,
  380. .remove = __devexit_p(tsl2550_remove),
  381. .id_table = tsl2550_id,
  382. };
  383. static int __init tsl2550_init(void)
  384. {
  385. return i2c_add_driver(&tsl2550_driver);
  386. }
  387. static void __exit tsl2550_exit(void)
  388. {
  389. i2c_del_driver(&tsl2550_driver);
  390. }
  391. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  392. MODULE_DESCRIPTION("TSL2550 ambient light sensor driver");
  393. MODULE_LICENSE("GPL");
  394. MODULE_VERSION(DRIVER_VERSION);
  395. module_init(tsl2550_init);
  396. module_exit(tsl2550_exit);