tsl2550.c 10 KB

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