leds-lp3944.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/leds-lp3944.h>
  33. /* Read Only Registers */
  34. #define LP3944_REG_INPUT1 0x00 /* LEDs 0-7 InputRegister (Read Only) */
  35. #define LP3944_REG_REGISTER1 0x01 /* None (Read Only) */
  36. #define LP3944_REG_PSC0 0x02 /* Frequency Prescaler 0 (R/W) */
  37. #define LP3944_REG_PWM0 0x03 /* PWM Register 0 (R/W) */
  38. #define LP3944_REG_PSC1 0x04 /* Frequency Prescaler 1 (R/W) */
  39. #define LP3944_REG_PWM1 0x05 /* PWM Register 1 (R/W) */
  40. #define LP3944_REG_LS0 0x06 /* LEDs 0-3 Selector (R/W) */
  41. #define LP3944_REG_LS1 0x07 /* LEDs 4-7 Selector (R/W) */
  42. /* These registers are not used to control leds in LP3944, they can store
  43. * arbitrary values which the chip will ignore.
  44. */
  45. #define LP3944_REG_REGISTER8 0x08
  46. #define LP3944_REG_REGISTER9 0x09
  47. #define LP3944_DIM0 0
  48. #define LP3944_DIM1 1
  49. /* period in ms */
  50. #define LP3944_PERIOD_MIN 0
  51. #define LP3944_PERIOD_MAX 1600
  52. /* duty cycle is a percentage */
  53. #define LP3944_DUTY_CYCLE_MIN 0
  54. #define LP3944_DUTY_CYCLE_MAX 100
  55. #define ldev_to_led(c) container_of(c, struct lp3944_led_data, ldev)
  56. /* Saved data */
  57. struct lp3944_led_data {
  58. u8 id;
  59. enum lp3944_type type;
  60. struct led_classdev ldev;
  61. struct i2c_client *client;
  62. };
  63. struct lp3944_data {
  64. struct mutex lock;
  65. struct i2c_client *client;
  66. struct lp3944_led_data leds[LP3944_LEDS_MAX];
  67. };
  68. static int lp3944_reg_read(struct i2c_client *client, u8 reg, u8 *value)
  69. {
  70. int tmp;
  71. tmp = i2c_smbus_read_byte_data(client, reg);
  72. if (tmp < 0)
  73. return tmp;
  74. *value = tmp;
  75. return 0;
  76. }
  77. static int lp3944_reg_write(struct i2c_client *client, u8 reg, u8 value)
  78. {
  79. return i2c_smbus_write_byte_data(client, reg, value);
  80. }
  81. /**
  82. * Set the period for DIM status
  83. *
  84. * @client: the i2c client
  85. * @dim: either LP3944_DIM0 or LP3944_DIM1
  86. * @period: period of a blink, that is a on/off cycle, expressed in ms.
  87. */
  88. static int lp3944_dim_set_period(struct i2c_client *client, u8 dim, u16 period)
  89. {
  90. u8 psc_reg;
  91. u8 psc_value;
  92. int err;
  93. if (dim == LP3944_DIM0)
  94. psc_reg = LP3944_REG_PSC0;
  95. else if (dim == LP3944_DIM1)
  96. psc_reg = LP3944_REG_PSC1;
  97. else
  98. return -EINVAL;
  99. /* Convert period to Prescaler value */
  100. if (period > LP3944_PERIOD_MAX)
  101. return -EINVAL;
  102. psc_value = (period * 255) / LP3944_PERIOD_MAX;
  103. err = lp3944_reg_write(client, psc_reg, psc_value);
  104. return err;
  105. }
  106. /**
  107. * Set the duty cycle for DIM status
  108. *
  109. * @client: the i2c client
  110. * @dim: either LP3944_DIM0 or LP3944_DIM1
  111. * @duty_cycle: percentage of a period during which a led is ON
  112. */
  113. static int lp3944_dim_set_dutycycle(struct i2c_client *client, u8 dim,
  114. u8 duty_cycle)
  115. {
  116. u8 pwm_reg;
  117. u8 pwm_value;
  118. int err;
  119. if (dim == LP3944_DIM0)
  120. pwm_reg = LP3944_REG_PWM0;
  121. else if (dim == LP3944_DIM1)
  122. pwm_reg = LP3944_REG_PWM1;
  123. else
  124. return -EINVAL;
  125. /* Convert duty cycle to PWM value */
  126. if (duty_cycle > LP3944_DUTY_CYCLE_MAX)
  127. return -EINVAL;
  128. pwm_value = (duty_cycle * 255) / LP3944_DUTY_CYCLE_MAX;
  129. err = lp3944_reg_write(client, pwm_reg, pwm_value);
  130. return err;
  131. }
  132. /**
  133. * Set the led status
  134. *
  135. * @led: a lp3944_led_data structure
  136. * @status: one of LP3944_LED_STATUS_OFF
  137. * LP3944_LED_STATUS_ON
  138. * LP3944_LED_STATUS_DIM0
  139. * LP3944_LED_STATUS_DIM1
  140. */
  141. static int lp3944_led_set(struct lp3944_led_data *led, u8 status)
  142. {
  143. struct lp3944_data *data = i2c_get_clientdata(led->client);
  144. u8 id = led->id;
  145. u8 reg;
  146. u8 val = 0;
  147. int err;
  148. dev_dbg(&led->client->dev, "%s: %s, status before normalization:%d\n",
  149. __func__, led->ldev.name, status);
  150. switch (id) {
  151. case LP3944_LED0:
  152. case LP3944_LED1:
  153. case LP3944_LED2:
  154. case LP3944_LED3:
  155. reg = LP3944_REG_LS0;
  156. break;
  157. case LP3944_LED4:
  158. case LP3944_LED5:
  159. case LP3944_LED6:
  160. case LP3944_LED7:
  161. id -= LP3944_LED4;
  162. reg = LP3944_REG_LS1;
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. if (status > LP3944_LED_STATUS_DIM1)
  168. return -EINVAL;
  169. /*
  170. * Invert status only when it's < 2 (i.e. 0 or 1) which means it's
  171. * controlling the on/off state directly.
  172. * When, instead, status is >= 2 don't invert it because it would mean
  173. * to mess with the hardware blinking mode.
  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. lp3944_led_set(led, LP3944_LED_STATUS_DIM0);
  231. return 0;
  232. }
  233. static int lp3944_led_set_brightness(struct led_classdev *led_cdev,
  234. enum led_brightness brightness)
  235. {
  236. struct lp3944_led_data *led = ldev_to_led(led_cdev);
  237. dev_dbg(&led->client->dev, "%s: %s, %d\n",
  238. __func__, led_cdev->name, brightness);
  239. return lp3944_led_set(led, !!brightness);
  240. }
  241. static int lp3944_configure(struct i2c_client *client,
  242. struct lp3944_data *data,
  243. struct lp3944_platform_data *pdata)
  244. {
  245. int i, err = 0;
  246. for (i = 0; i < pdata->leds_size; i++) {
  247. struct lp3944_led *pled = &pdata->leds[i];
  248. struct lp3944_led_data *led = &data->leds[i];
  249. led->client = client;
  250. led->id = i;
  251. switch (pled->type) {
  252. case LP3944_LED_TYPE_LED:
  253. case LP3944_LED_TYPE_LED_INVERTED:
  254. led->type = pled->type;
  255. led->ldev.name = pled->name;
  256. led->ldev.max_brightness = 1;
  257. led->ldev.brightness_set_blocking =
  258. lp3944_led_set_brightness;
  259. led->ldev.blink_set = lp3944_led_set_blink;
  260. led->ldev.flags = LED_CORE_SUSPENDRESUME;
  261. err = led_classdev_register(&client->dev, &led->ldev);
  262. if (err < 0) {
  263. dev_err(&client->dev,
  264. "couldn't register LED %s\n",
  265. led->ldev.name);
  266. goto exit;
  267. }
  268. /* to expose the default value to userspace */
  269. led->ldev.brightness =
  270. (enum led_brightness) pled->status;
  271. /* Set the default led status */
  272. err = lp3944_led_set(led, pled->status);
  273. if (err < 0) {
  274. dev_err(&client->dev,
  275. "%s couldn't set STATUS %d\n",
  276. led->ldev.name, pled->status);
  277. goto exit;
  278. }
  279. break;
  280. case LP3944_LED_TYPE_NONE:
  281. default:
  282. break;
  283. }
  284. }
  285. return 0;
  286. exit:
  287. if (i > 0)
  288. for (i = i - 1; i >= 0; i--)
  289. switch (pdata->leds[i].type) {
  290. case LP3944_LED_TYPE_LED:
  291. case LP3944_LED_TYPE_LED_INVERTED:
  292. led_classdev_unregister(&data->leds[i].ldev);
  293. break;
  294. case LP3944_LED_TYPE_NONE:
  295. default:
  296. break;
  297. }
  298. return err;
  299. }
  300. static int lp3944_probe(struct i2c_client *client,
  301. const struct i2c_device_id *id)
  302. {
  303. struct lp3944_platform_data *lp3944_pdata =
  304. dev_get_platdata(&client->dev);
  305. struct lp3944_data *data;
  306. int err;
  307. if (lp3944_pdata == NULL) {
  308. dev_err(&client->dev, "no platform data\n");
  309. return -EINVAL;
  310. }
  311. /* Let's see whether this adapter can support what we need. */
  312. if (!i2c_check_functionality(client->adapter,
  313. I2C_FUNC_SMBUS_BYTE_DATA)) {
  314. dev_err(&client->dev, "insufficient functionality!\n");
  315. return -ENODEV;
  316. }
  317. data = devm_kzalloc(&client->dev, sizeof(struct lp3944_data),
  318. GFP_KERNEL);
  319. if (!data)
  320. return -ENOMEM;
  321. data->client = client;
  322. i2c_set_clientdata(client, data);
  323. mutex_init(&data->lock);
  324. err = lp3944_configure(client, data, lp3944_pdata);
  325. if (err < 0)
  326. return err;
  327. dev_info(&client->dev, "lp3944 enabled\n");
  328. return 0;
  329. }
  330. static int lp3944_remove(struct i2c_client *client)
  331. {
  332. struct lp3944_platform_data *pdata = dev_get_platdata(&client->dev);
  333. struct lp3944_data *data = i2c_get_clientdata(client);
  334. int i;
  335. for (i = 0; i < pdata->leds_size; i++)
  336. switch (data->leds[i].type) {
  337. case LP3944_LED_TYPE_LED:
  338. case LP3944_LED_TYPE_LED_INVERTED:
  339. led_classdev_unregister(&data->leds[i].ldev);
  340. break;
  341. case LP3944_LED_TYPE_NONE:
  342. default:
  343. break;
  344. }
  345. return 0;
  346. }
  347. /* lp3944 i2c driver struct */
  348. static const struct i2c_device_id lp3944_id[] = {
  349. {"lp3944", 0},
  350. {}
  351. };
  352. MODULE_DEVICE_TABLE(i2c, lp3944_id);
  353. static struct i2c_driver lp3944_driver = {
  354. .driver = {
  355. .name = "lp3944",
  356. },
  357. .probe = lp3944_probe,
  358. .remove = lp3944_remove,
  359. .id_table = lp3944_id,
  360. };
  361. module_i2c_driver(lp3944_driver);
  362. MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
  363. MODULE_DESCRIPTION("LP3944 Fun Light Chip");
  364. MODULE_LICENSE("GPL");