leds-lp3944.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * leds-lp3944.c - driver for National Semiconductor LP3944 Funlight Chip
  3. *
  4. * Copyright (C) 2009 Antonio Ospite <ospite@studenti.unina.it>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. /*
  12. * I2C driver for National Semiconductor LP3944 Funlight Chip
  13. * http://www.national.com/pf/LP/LP3944.html
  14. *
  15. * This helper chip can drive up to 8 leds, with two programmable DIM modes;
  16. * it could even be used as a gpio expander but this driver assumes it is used
  17. * as a led controller.
  18. *
  19. * The DIM modes are used to set _blink_ patterns for leds, the pattern is
  20. * specified supplying two parameters:
  21. * - period: from 0s to 1.6s
  22. * - duty cycle: percentage of the period the led is on, from 0 to 100
  23. *
  24. * LP3944 can be found on Motorola A910 smartphone, where it drives the rgb
  25. * leds, the camera flash light and the displays backlights.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/i2c.h>
  29. #include <linux/slab.h>
  30. #include <linux/leds.h>
  31. #include <linux/mutex.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/leds-lp3944.h>
  34. /* Read Only Registers */
  35. #define LP3944_REG_INPUT1 0x00 /* LEDs 0-7 InputRegister (Read Only) */
  36. #define LP3944_REG_REGISTER1 0x01 /* None (Read Only) */
  37. #define LP3944_REG_PSC0 0x02 /* Frequency Prescaler 0 (R/W) */
  38. #define LP3944_REG_PWM0 0x03 /* PWM Register 0 (R/W) */
  39. #define LP3944_REG_PSC1 0x04 /* Frequency Prescaler 1 (R/W) */
  40. #define LP3944_REG_PWM1 0x05 /* PWM Register 1 (R/W) */
  41. #define LP3944_REG_LS0 0x06 /* LEDs 0-3 Selector (R/W) */
  42. #define LP3944_REG_LS1 0x07 /* LEDs 4-7 Selector (R/W) */
  43. /* These registers are not used to control leds in LP3944, they can store
  44. * arbitrary values which the chip will ignore.
  45. */
  46. #define LP3944_REG_REGISTER8 0x08
  47. #define LP3944_REG_REGISTER9 0x09
  48. #define LP3944_DIM0 0
  49. #define LP3944_DIM1 1
  50. /* period in ms */
  51. #define LP3944_PERIOD_MIN 0
  52. #define LP3944_PERIOD_MAX 1600
  53. /* duty cycle is a percentage */
  54. #define LP3944_DUTY_CYCLE_MIN 0
  55. #define LP3944_DUTY_CYCLE_MAX 100
  56. #define ldev_to_led(c) container_of(c, struct lp3944_led_data, ldev)
  57. /* Saved data */
  58. struct lp3944_led_data {
  59. u8 id;
  60. enum lp3944_type type;
  61. enum lp3944_status status;
  62. struct led_classdev ldev;
  63. struct i2c_client *client;
  64. struct work_struct work;
  65. };
  66. struct lp3944_data {
  67. struct mutex lock;
  68. struct i2c_client *client;
  69. struct lp3944_led_data leds[LP3944_LEDS_MAX];
  70. };
  71. static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value)
  72. {
  73. int tmp;
  74. tmp = i2c_smbus_read_byte_data(client, reg);
  75. if (tmp < 0)
  76. return -EINVAL;
  77. *value = tmp;
  78. return 0;
  79. }
  80. static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
  81. {
  82. return i2c_smbus_write_byte_data(client, reg, value);
  83. }
  84. /**
  85. * Set the period for DIM status
  86. *
  87. * @client: the i2c client
  88. * @dim: either LP3944_DIM0 or LP3944_DIM1
  89. * @period: period of a blink, that is a on/off cycle, expressed in ms.
  90. */
  91. static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period)
  92. {
  93. u8 psc_reg;
  94. u8 psc_value;
  95. int err;
  96. if (dim == LP3944_DIM0)
  97. psc_reg = LP3944_REG_PSC0;
  98. else if (dim == LP3944_DIM1)
  99. psc_reg = LP3944_REG_PSC1;
  100. else
  101. return -EINVAL;
  102. /* Convert period to Prescaler value */
  103. if (period > LP3944_PERIOD_MAX)
  104. return -EINVAL;
  105. psc_value = (period * 255) / LP3944_PERIOD_MAX;
  106. err = lp3944_reg_write(client, psc_reg, psc_value);
  107. return err;
  108. }
  109. /**
  110. * Set the duty cycle for DIM status
  111. *
  112. * @client: the i2c client
  113. * @dim: either LP3944_DIM0 or LP3944_DIM1
  114. * @duty_cycle: percentage of a period during which a led is ON
  115. */
  116. static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim,
  117. u8 duty_cycle)
  118. {
  119. u8 pwm_reg;
  120. u8 pwm_value;
  121. int err;
  122. if (dim == LP3944_DIM0)
  123. pwm_reg = LP3944_REG_PWM0;
  124. else if (dim == LP3944_DIM1)
  125. pwm_reg = LP3944_REG_PWM1;
  126. else
  127. return -EINVAL;
  128. /* Convert duty cycle to PWM value */
  129. if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
  130. return -EINVAL;
  131. pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
  132. err = lp3944_reg_write(client, pwm_reg, pwm_value);
  133. return err;
  134. }
  135. /**
  136. * Set the led status
  137. *
  138. * @led: a lp3944_led_data structure
  139. * @status: one of LP3944_LED_STATUS_OFF
  140. * LP3944_LED_STATUS_ON
  141. * LP3944_LED_STATUS_DIM0
  142. * LP3944_LED_STATUS_DIM1
  143. */
  144. static int lp3944_led_set(struct lp3944_led_data *led, u8 status)
  145. {
  146. struct lp3944_data *data = i2c_get_clientdata(led->client);
  147. u8 id = led->id;
  148. u8 reg;
  149. u8 val = 0;
  150. int err;
  151. dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
  152. __func__, led->ldev.name, status);
  153. switch (id) {
  154. case LP3944_LED0:
  155. case LP3944_LED1:
  156. case LP3944_LED2:
  157. case LP3944_LED3:
  158. reg = LP3944_REG_LS0;
  159. break;
  160. case LP3944_LED4:
  161. case LP3944_LED5:
  162. case LP3944_LED6:
  163. case LP3944_LED7:
  164. id -= LP3944_LED4;
  165. reg = LP3944_REG_LS1;
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. if (status > LP3944_LED_STATUS_DIM1)
  171. return -EINVAL;
  172. /* invert only 0 and 1, leave unchanged the other values,
  173. * remember we are abusing status to set blink patterns
  174. */
  175. if (led->type == LP3944_LED_TYPE_LED_INVERTED && status < 2)
  176. status = 1 - status;
  177. mutex_lock(&data->lock);
  178. lp3944_reg_read(led->client, reg, &val);
  179. val &= ~(LP3944_LED_STATUS_MASK << (id << 1));
  180. val |= (status << (id << 1));
  181. dev_dbg(&led->client->dev, "%s: %s, reg:%d id:%d status:%d val:%#x\n",
  182. __func__, led->ldev.name, reg, id, status, val);
  183. /* set led status */
  184. err = lp3944_reg_write(led->client, reg, val);
  185. mutex_unlock(&data->lock);
  186. return err;
  187. }
  188. static int lp3944_led_set_blink(struct led_classdev *led_cdev,
  189. unsigned long *delay_on,
  190. unsigned long *delay_off)
  191. {
  192. struct lp3944_led_data *led = ldev_to_led(led_cdev);
  193. u16 period;
  194. u8 duty_cycle;
  195. int err;
  196. /* units are in ms */
  197. if (*delay_on + *delay_off > LP3944_PERIOD_MAX)
  198. return -EINVAL;
  199. if (*delay_on == 0 && *delay_off == 0) {
  200. /* Special case: the leds subsystem requires a default user
  201. * friendly blink pattern for the LED. Let's blink the led
  202. * slowly (1Hz).
  203. */
  204. *delay_on = 500;
  205. *delay_off = 500;
  206. }
  207. period = (*delay_on) + (*delay_off);
  208. /* duty_cycle is the percentage of period during which the led is ON */
  209. duty_cycle = 100 * (*delay_on) / period;
  210. /* invert duty cycle for inverted leds, this has the same effect of
  211. * swapping delay_on and delay_off
  212. */
  213. if (led->type == LP3944_LED_TYPE_LED_INVERTED)
  214. duty_cycle = 100 - duty_cycle;
  215. /* NOTE: using always the first DIM mode, this means that all leds
  216. * will have the same blinking pattern.
  217. *
  218. * We could find a way later to have two leds blinking in hardware
  219. * with different patterns at the same time, falling back to software
  220. * control for the other ones.
  221. */
  222. err = lp3944_dim_set_period(led->client, LP3944_DIM0, period);
  223. if (err)
  224. return err;
  225. err = lp3944_dim_set_dutycycle(led->client, LP3944_DIM0, duty_cycle);
  226. if (err)
  227. return err;
  228. dev_dbg(&led->client->dev, "%s: OK hardware accelerated blink!\n",
  229. __func__);
  230. led->status = LP3944_LED_STATUS_DIM0;
  231. schedule_work(&led->work);
  232. return 0;
  233. }
  234. static void lp3944_led_set_brightness(struct led_classdev *led_cdev,
  235. enum led_brightness brightness)
  236. {
  237. struct lp3944_led_data *led = ldev_to_led(led_cdev);
  238. dev_dbg(&led->client->dev, "%s: %s, %d\n",
  239. __func__, led_cdev->name, brightness);
  240. led->status = brightness;
  241. schedule_work(&led->work);
  242. }
  243. static void lp3944_led_work(struct work_struct *work)
  244. {
  245. struct lp3944_led_data *led;
  246. led = container_of(work, struct lp3944_led_data, work);
  247. lp3944_led_set(led, led->status);
  248. }
  249. static int lp3944_configure(struct i2c_client *client,
  250. struct lp3944_data *data,
  251. struct lp3944_platform_data *pdata)
  252. {
  253. int i, err = 0;
  254. for (i = 0; i < pdata->leds_size; i++) {
  255. struct lp3944_led *pled = &pdata->leds[i];
  256. struct lp3944_led_data *led = &data->leds[i];
  257. led->client = client;
  258. led->id = i;
  259. switch (pled->type) {
  260. case LP3944_LED_TYPE_LED:
  261. case LP3944_LED_TYPE_LED_INVERTED:
  262. led->type = pled->type;
  263. led->status = pled->status;
  264. led->ldev.name = pled->name;
  265. led->ldev.max_brightness = 1;
  266. led->ldev.brightness_set = lp3944_led_set_brightness;
  267. led->ldev.blink_set = lp3944_led_set_blink;
  268. led->ldev.flags = LED_CORE_SUSPENDRESUME;
  269. INIT_WORK(&led->work, lp3944_led_work);
  270. err = led_classdev_register(&client->dev, &led->ldev);
  271. if (err < 0) {
  272. dev_err(&client->dev,
  273. "couldn't register LED %s\n",
  274. led->ldev.name);
  275. goto exit;
  276. }
  277. /* to expose the default value to userspace */
  278. led->ldev.brightness = led->status;
  279. /* Set the default led status */
  280. err = lp3944_led_set(led, led->status);
  281. if (err < 0) {
  282. dev_err(&client->dev,
  283. "%s couldn't set STATUS %d\n",
  284. led->ldev.name, led->status);
  285. goto exit;
  286. }
  287. break;
  288. case LP3944_LED_TYPE_NONE:
  289. default:
  290. break;
  291. }
  292. }
  293. return 0;
  294. exit:
  295. if (i > 0)
  296. for (i = i - 1; i >= 0; i--)
  297. switch (pdata->leds[i].type) {
  298. case LP3944_LED_TYPE_LED:
  299. case LP3944_LED_TYPE_LED_INVERTED:
  300. led_classdev_unregister(&data->leds[i].ldev);
  301. cancel_work_sync(&data->leds[i].work);
  302. break;
  303. case LP3944_LED_TYPE_NONE:
  304. default:
  305. break;
  306. }
  307. return err;
  308. }
  309. static int __devinit lp3944_probe(struct i2c_client *client,
  310. const struct i2c_device_id *id)
  311. {
  312. struct lp3944_platform_data *lp3944_pdata = client->dev.platform_data;
  313. struct lp3944_data *data;
  314. int err;
  315. if (lp3944_pdata == NULL) {
  316. dev_err(&client->dev, "no platform data\n");
  317. return -EINVAL;
  318. }
  319. /* Let's see whether this adapter can support what we need. */
  320. if (!i2c_check_functionality(client->adapter,
  321. I2C_FUNC_SMBUS_BYTE_DATA)) {
  322. dev_err(&client->dev, "insufficient functionality!\n");
  323. return -ENODEV;
  324. }
  325. data = kzalloc(sizeof(struct lp3944_data), GFP_KERNEL);
  326. if (!data)
  327. return -ENOMEM;
  328. data->client = client;
  329. i2c_set_clientdata(client, data);
  330. mutex_init(&data->lock);
  331. err = lp3944_configure(client, data, lp3944_pdata);
  332. if (err < 0) {
  333. kfree(data);
  334. return err;
  335. }
  336. dev_info(&client->dev, "lp3944 enabled\n");
  337. return 0;
  338. }
  339. static int __devexit lp3944_remove(struct i2c_client *client)
  340. {
  341. struct lp3944_platform_data *pdata = client->dev.platform_data;
  342. struct lp3944_data *data = i2c_get_clientdata(client);
  343. int i;
  344. for (i = 0; i < pdata->leds_size; i++)
  345. switch (data->leds[i].type) {
  346. case LP3944_LED_TYPE_LED:
  347. case LP3944_LED_TYPE_LED_INVERTED:
  348. led_classdev_unregister(&data->leds[i].ldev);
  349. cancel_work_sync(&data->leds[i].work);
  350. break;
  351. case LP3944_LED_TYPE_NONE:
  352. default:
  353. break;
  354. }
  355. kfree(data);
  356. return 0;
  357. }
  358. /* lp3944 i2c driver struct */
  359. static const struct i2c_device_id lp3944_id[] = {
  360. {"lp3944", 0},
  361. {}
  362. };
  363. MODULE_DEVICE_TABLE(i2c, lp3944_id);
  364. static struct i2c_driver lp3944_driver = {
  365. .driver = {
  366. .name = "lp3944",
  367. },
  368. .probe = lp3944_probe,
  369. .remove = __devexit_p(lp3944_remove),
  370. .id_table = lp3944_id,
  371. };
  372. static int __init lp3944_module_init(void)
  373. {
  374. return i2c_add_driver(&lp3944_driver);
  375. }
  376. static void __exit lp3944_module_exit(void)
  377. {
  378. i2c_del_driver(&lp3944_driver);
  379. }
  380. module_init(lp3944_module_init);
  381. module_exit(lp3944_module_exit);
  382. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  383. MODULE_DESCRIPTION("LP3944 Fun Light Chip");
  384. MODULE_LICENSE("GPL");