tsl2550.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. u8 ch0, ch1;
  211. int ret;
  212. ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC0);
  213. if (ret < 0)
  214. return ret;
  215. ch0 = ret;
  216. ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC1);
  217. if (ret < 0)
  218. return ret;
  219. ch1 = ret;
  220. /* Do the job */
  221. ret = tsl2550_calculate_lux(ch0, ch1);
  222. if (ret < 0)
  223. return ret;
  224. return sprintf(buf, "%d\n", ret);
  225. }
  226. static ssize_t tsl2550_show_lux1_input(struct device *dev,
  227. struct device_attribute *attr, char *buf)
  228. {
  229. struct i2c_client *client = to_i2c_client(dev);
  230. struct tsl2550_data *data = i2c_get_clientdata(client);
  231. int ret;
  232. /* No LUX data if not operational */
  233. if (!data->power_state)
  234. return -EBUSY;
  235. mutex_lock(&data->update_lock);
  236. ret = __tsl2550_show_lux(client, buf);
  237. mutex_unlock(&data->update_lock);
  238. return ret;
  239. }
  240. static DEVICE_ATTR(lux1_input, S_IRUGO,
  241. tsl2550_show_lux1_input, NULL);
  242. static struct attribute *tsl2550_attributes[] = {
  243. &dev_attr_power_state.attr,
  244. &dev_attr_operating_mode.attr,
  245. &dev_attr_lux1_input.attr,
  246. NULL
  247. };
  248. static const struct attribute_group tsl2550_attr_group = {
  249. .attrs = tsl2550_attributes,
  250. };
  251. /*
  252. * Initialization function
  253. */
  254. static int tsl2550_init_client(struct i2c_client *client)
  255. {
  256. struct tsl2550_data *data = i2c_get_clientdata(client);
  257. int err;
  258. /*
  259. * Probe the chip. To do so we try to power up the device and then to
  260. * read back the 0x03 code
  261. */
  262. err = i2c_smbus_read_byte_data(client, TSL2550_POWER_UP);
  263. if (err < 0)
  264. return err;
  265. if (err != TSL2550_POWER_UP)
  266. return -ENODEV;
  267. data->power_state = 1;
  268. /* Set the default operating mode */
  269. err = i2c_smbus_write_byte(client,
  270. TSL2550_MODE_RANGE[data->operating_mode]);
  271. if (err < 0)
  272. return err;
  273. return 0;
  274. }
  275. /*
  276. * I2C init/probing/exit functions
  277. */
  278. static struct i2c_driver tsl2550_driver;
  279. static int __devinit tsl2550_probe(struct i2c_client *client,
  280. const struct i2c_device_id *id)
  281. {
  282. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  283. struct tsl2550_data *data;
  284. int *opmode, err = 0;
  285. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE
  286. | I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
  287. err = -EIO;
  288. goto exit;
  289. }
  290. data = kzalloc(sizeof(struct tsl2550_data), GFP_KERNEL);
  291. if (!data) {
  292. err = -ENOMEM;
  293. goto exit;
  294. }
  295. data->client = client;
  296. i2c_set_clientdata(client, data);
  297. /* Check platform data */
  298. opmode = client->dev.platform_data;
  299. if (opmode) {
  300. if (*opmode < 0 || *opmode > 1) {
  301. dev_err(&client->dev, "invalid operating_mode (%d)\n",
  302. *opmode);
  303. err = -EINVAL;
  304. goto exit_kfree;
  305. }
  306. data->operating_mode = *opmode;
  307. } else
  308. data->operating_mode = 0; /* default mode is standard */
  309. dev_info(&client->dev, "%s operating mode\n",
  310. data->operating_mode ? "extended" : "standard");
  311. mutex_init(&data->update_lock);
  312. /* Initialize the TSL2550 chip */
  313. err = tsl2550_init_client(client);
  314. if (err)
  315. goto exit_kfree;
  316. /* Register sysfs hooks */
  317. err = sysfs_create_group(&client->dev.kobj, &tsl2550_attr_group);
  318. if (err)
  319. goto exit_kfree;
  320. dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
  321. return 0;
  322. exit_kfree:
  323. kfree(data);
  324. exit:
  325. return err;
  326. }
  327. static int __devexit tsl2550_remove(struct i2c_client *client)
  328. {
  329. sysfs_remove_group(&client->dev.kobj, &tsl2550_attr_group);
  330. /* Power down the device */
  331. tsl2550_set_power_state(client, 0);
  332. kfree(i2c_get_clientdata(client));
  333. return 0;
  334. }
  335. #ifdef CONFIG_PM
  336. static int tsl2550_suspend(struct i2c_client *client, pm_message_t mesg)
  337. {
  338. return tsl2550_set_power_state(client, 0);
  339. }
  340. static int tsl2550_resume(struct i2c_client *client)
  341. {
  342. return tsl2550_set_power_state(client, 1);
  343. }
  344. #else
  345. #define tsl2550_suspend NULL
  346. #define tsl2550_resume NULL
  347. #endif /* CONFIG_PM */
  348. static const struct i2c_device_id tsl2550_id[] = {
  349. { "tsl2550", 0 },
  350. { }
  351. };
  352. MODULE_DEVICE_TABLE(i2c, tsl2550_id);
  353. static struct i2c_driver tsl2550_driver = {
  354. .driver = {
  355. .name = TSL2550_DRV_NAME,
  356. .owner = THIS_MODULE,
  357. },
  358. .suspend = tsl2550_suspend,
  359. .resume = tsl2550_resume,
  360. .probe = tsl2550_probe,
  361. .remove = __devexit_p(tsl2550_remove),
  362. .id_table = tsl2550_id,
  363. };
  364. static int __init tsl2550_init(void)
  365. {
  366. return i2c_add_driver(&tsl2550_driver);
  367. }
  368. static void __exit tsl2550_exit(void)
  369. {
  370. i2c_del_driver(&tsl2550_driver);
  371. }
  372. MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
  373. MODULE_DESCRIPTION("TSL2550 ambient light sensor driver");
  374. MODULE_LICENSE("GPL");
  375. MODULE_VERSION(DRIVER_VERSION);
  376. module_init(tsl2550_init);
  377. module_exit(tsl2550_exit);