leds-pca963x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright 2011 bct electronic GmbH
  3. * Copyright 2013 Qtechnology/AS
  4. *
  5. * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
  6. * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
  7. *
  8. * Based on leds-pca955x.c
  9. *
  10. * This file is subject to the terms and conditions of version 2 of
  11. * the GNU General Public License. See the file COPYING in the main
  12. * directory of this archive for more details.
  13. *
  14. * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
  15. * LED driver for the PCA9634 I2C LED driver (7-bit slave address set by hw.)
  16. *
  17. * Note that hardware blinking violates the leds infrastructure driver
  18. * interface since the hardware only supports blinking all LEDs with the
  19. * same delay_on/delay_off rates. That is, only the LEDs that are set to
  20. * blink will actually blink but all LEDs that are set to blink will blink
  21. * in identical fashion. The delay_on/delay_off values of the last LED
  22. * that is set to blink will be used for all of the blinking LEDs.
  23. * Hardware blinking is disabled by default but can be enabled by setting
  24. * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
  25. * or by adding the 'nxp,hw-blink' property to the DTS.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/delay.h>
  29. #include <linux/string.h>
  30. #include <linux/ctype.h>
  31. #include <linux/leds.h>
  32. #include <linux/err.h>
  33. #include <linux/i2c.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/slab.h>
  36. #include <linux/of.h>
  37. #include <linux/platform_data/leds-pca963x.h>
  38. /* LED select registers determine the source that drives LED outputs */
  39. #define PCA963X_LED_OFF 0x0 /* LED driver off */
  40. #define PCA963X_LED_ON 0x1 /* LED driver on */
  41. #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
  42. #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
  43. #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
  44. #define PCA963X_MODE1 0x00
  45. #define PCA963X_MODE2 0x01
  46. #define PCA963X_PWM_BASE 0x02
  47. enum pca963x_type {
  48. pca9633,
  49. pca9634,
  50. };
  51. struct pca963x_chipdef {
  52. u8 grppwm;
  53. u8 grpfreq;
  54. u8 ledout_base;
  55. int n_leds;
  56. };
  57. static struct pca963x_chipdef pca963x_chipdefs[] = {
  58. [pca9633] = {
  59. .grppwm = 0x6,
  60. .grpfreq = 0x7,
  61. .ledout_base = 0x8,
  62. .n_leds = 4,
  63. },
  64. [pca9634] = {
  65. .grppwm = 0xa,
  66. .grpfreq = 0xb,
  67. .ledout_base = 0xc,
  68. .n_leds = 8,
  69. },
  70. };
  71. /* Total blink period in milliseconds */
  72. #define PCA963X_BLINK_PERIOD_MIN 42
  73. #define PCA963X_BLINK_PERIOD_MAX 10667
  74. static const struct i2c_device_id pca963x_id[] = {
  75. { "pca9632", pca9633 },
  76. { "pca9633", pca9633 },
  77. { "pca9634", pca9634 },
  78. { }
  79. };
  80. MODULE_DEVICE_TABLE(i2c, pca963x_id);
  81. enum pca963x_cmd {
  82. BRIGHTNESS_SET,
  83. BLINK_SET,
  84. };
  85. struct pca963x_led;
  86. struct pca963x {
  87. struct pca963x_chipdef *chipdef;
  88. struct mutex mutex;
  89. struct i2c_client *client;
  90. struct pca963x_led *leds;
  91. };
  92. struct pca963x_led {
  93. struct pca963x *chip;
  94. struct work_struct work;
  95. enum led_brightness brightness;
  96. struct led_classdev led_cdev;
  97. int led_num; /* 0 .. 7 potentially */
  98. enum pca963x_cmd cmd;
  99. char name[32];
  100. u8 gdc;
  101. u8 gfrq;
  102. };
  103. static void pca963x_brightness_work(struct pca963x_led *pca963x)
  104. {
  105. u8 ledout_addr = pca963x->chip->chipdef->ledout_base
  106. + (pca963x->led_num / 4);
  107. u8 ledout;
  108. int shift = 2 * (pca963x->led_num % 4);
  109. u8 mask = 0x3 << shift;
  110. mutex_lock(&pca963x->chip->mutex);
  111. ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
  112. switch (pca963x->brightness) {
  113. case LED_FULL:
  114. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  115. (ledout & ~mask) | (PCA963X_LED_ON << shift));
  116. break;
  117. case LED_OFF:
  118. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  119. ledout & ~mask);
  120. break;
  121. default:
  122. i2c_smbus_write_byte_data(pca963x->chip->client,
  123. PCA963X_PWM_BASE + pca963x->led_num,
  124. pca963x->brightness);
  125. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  126. (ledout & ~mask) | (PCA963X_LED_PWM << shift));
  127. break;
  128. }
  129. mutex_unlock(&pca963x->chip->mutex);
  130. }
  131. static void pca963x_blink_work(struct pca963x_led *pca963x)
  132. {
  133. u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
  134. (pca963x->led_num / 4);
  135. u8 ledout;
  136. u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
  137. PCA963X_MODE2);
  138. int shift = 2 * (pca963x->led_num % 4);
  139. u8 mask = 0x3 << shift;
  140. i2c_smbus_write_byte_data(pca963x->chip->client,
  141. pca963x->chip->chipdef->grppwm, pca963x->gdc);
  142. i2c_smbus_write_byte_data(pca963x->chip->client,
  143. pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
  144. if (!(mode2 & PCA963X_MODE2_DMBLNK))
  145. i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
  146. mode2 | PCA963X_MODE2_DMBLNK);
  147. mutex_lock(&pca963x->chip->mutex);
  148. ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
  149. if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift))
  150. i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
  151. (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift));
  152. mutex_unlock(&pca963x->chip->mutex);
  153. }
  154. static void pca963x_work(struct work_struct *work)
  155. {
  156. struct pca963x_led *pca963x = container_of(work,
  157. struct pca963x_led, work);
  158. switch (pca963x->cmd) {
  159. case BRIGHTNESS_SET:
  160. pca963x_brightness_work(pca963x);
  161. break;
  162. case BLINK_SET:
  163. pca963x_blink_work(pca963x);
  164. break;
  165. }
  166. }
  167. static void pca963x_led_set(struct led_classdev *led_cdev,
  168. enum led_brightness value)
  169. {
  170. struct pca963x_led *pca963x;
  171. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  172. pca963x->cmd = BRIGHTNESS_SET;
  173. pca963x->brightness = value;
  174. /*
  175. * Must use workqueue for the actual I/O since I2C operations
  176. * can sleep.
  177. */
  178. schedule_work(&pca963x->work);
  179. }
  180. static int pca963x_blink_set(struct led_classdev *led_cdev,
  181. unsigned long *delay_on, unsigned long *delay_off)
  182. {
  183. struct pca963x_led *pca963x;
  184. unsigned long time_on, time_off, period;
  185. u8 gdc, gfrq;
  186. pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
  187. time_on = *delay_on;
  188. time_off = *delay_off;
  189. /* If both zero, pick reasonable defaults of 500ms each */
  190. if (!time_on && !time_off) {
  191. time_on = 500;
  192. time_off = 500;
  193. }
  194. period = time_on + time_off;
  195. /* If period not supported by hardware, default to someting sane. */
  196. if ((period < PCA963X_BLINK_PERIOD_MIN) ||
  197. (period > PCA963X_BLINK_PERIOD_MAX)) {
  198. time_on = 500;
  199. time_off = 500;
  200. period = time_on + time_off;
  201. }
  202. /*
  203. * From manual: duty cycle = (GDC / 256) ->
  204. * (time_on / period) = (GDC / 256) ->
  205. * GDC = ((time_on * 256) / period)
  206. */
  207. gdc = (time_on * 256) / period;
  208. /*
  209. * From manual: period = ((GFRQ + 1) / 24) in seconds.
  210. * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
  211. * GFRQ = ((period * 24 / 1000) - 1)
  212. */
  213. gfrq = (period * 24 / 1000) - 1;
  214. pca963x->cmd = BLINK_SET;
  215. pca963x->gdc = gdc;
  216. pca963x->gfrq = gfrq;
  217. /*
  218. * Must use workqueue for the actual I/O since I2C operations
  219. * can sleep.
  220. */
  221. schedule_work(&pca963x->work);
  222. *delay_on = time_on;
  223. *delay_off = time_off;
  224. return 0;
  225. }
  226. #if IS_ENABLED(CONFIG_OF)
  227. static struct pca963x_platform_data *
  228. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  229. {
  230. struct device_node *np = client->dev.of_node, *child;
  231. struct pca963x_platform_data *pdata;
  232. struct led_info *pca963x_leds;
  233. int count;
  234. count = of_get_child_count(np);
  235. if (!count || count > chip->n_leds)
  236. return ERR_PTR(-ENODEV);
  237. pca963x_leds = devm_kzalloc(&client->dev,
  238. sizeof(struct led_info) * chip->n_leds, GFP_KERNEL);
  239. if (!pca963x_leds)
  240. return ERR_PTR(-ENOMEM);
  241. for_each_child_of_node(np, child) {
  242. struct led_info led;
  243. u32 reg;
  244. int res;
  245. res = of_property_read_u32(child, "reg", &reg);
  246. if ((res != 0) || (reg >= chip->n_leds))
  247. continue;
  248. led.name =
  249. of_get_property(child, "label", NULL) ? : child->name;
  250. led.default_trigger =
  251. of_get_property(child, "linux,default-trigger", NULL);
  252. pca963x_leds[reg] = led;
  253. }
  254. pdata = devm_kzalloc(&client->dev,
  255. sizeof(struct pca963x_platform_data), GFP_KERNEL);
  256. if (!pdata)
  257. return ERR_PTR(-ENOMEM);
  258. pdata->leds.leds = pca963x_leds;
  259. pdata->leds.num_leds = chip->n_leds;
  260. /* default to open-drain unless totem pole (push-pull) is specified */
  261. if (of_property_read_bool(np, "nxp,totem-pole"))
  262. pdata->outdrv = PCA963X_TOTEM_POLE;
  263. else
  264. pdata->outdrv = PCA963X_OPEN_DRAIN;
  265. /* default to software blinking unless hardware blinking is specified */
  266. if (of_property_read_bool(np, "nxp,hw-blink"))
  267. pdata->blink_type = PCA963X_HW_BLINK;
  268. else
  269. pdata->blink_type = PCA963X_SW_BLINK;
  270. return pdata;
  271. }
  272. static const struct of_device_id of_pca963x_match[] = {
  273. { .compatible = "nxp,pca9632", },
  274. { .compatible = "nxp,pca9633", },
  275. { .compatible = "nxp,pca9634", },
  276. {},
  277. };
  278. #else
  279. static struct pca963x_platform_data *
  280. pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
  281. {
  282. return ERR_PTR(-ENODEV);
  283. }
  284. #endif
  285. static int pca963x_probe(struct i2c_client *client,
  286. const struct i2c_device_id *id)
  287. {
  288. struct pca963x *pca963x_chip;
  289. struct pca963x_led *pca963x;
  290. struct pca963x_platform_data *pdata;
  291. struct pca963x_chipdef *chip;
  292. int i, err;
  293. chip = &pca963x_chipdefs[id->driver_data];
  294. pdata = dev_get_platdata(&client->dev);
  295. if (!pdata) {
  296. pdata = pca963x_dt_init(client, chip);
  297. if (IS_ERR(pdata)) {
  298. dev_warn(&client->dev, "could not parse configuration\n");
  299. pdata = NULL;
  300. }
  301. }
  302. if (pdata && (pdata->leds.num_leds < 1 ||
  303. pdata->leds.num_leds > chip->n_leds)) {
  304. dev_err(&client->dev, "board info must claim 1-%d LEDs",
  305. chip->n_leds);
  306. return -EINVAL;
  307. }
  308. pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
  309. GFP_KERNEL);
  310. if (!pca963x_chip)
  311. return -ENOMEM;
  312. pca963x = devm_kzalloc(&client->dev, chip->n_leds * sizeof(*pca963x),
  313. GFP_KERNEL);
  314. if (!pca963x)
  315. return -ENOMEM;
  316. i2c_set_clientdata(client, pca963x_chip);
  317. mutex_init(&pca963x_chip->mutex);
  318. pca963x_chip->chipdef = chip;
  319. pca963x_chip->client = client;
  320. pca963x_chip->leds = pca963x;
  321. /* Turn off LEDs by default*/
  322. i2c_smbus_write_byte_data(client, chip->ledout_base, 0x00);
  323. if (chip->n_leds > 4)
  324. i2c_smbus_write_byte_data(client, chip->ledout_base + 1, 0x00);
  325. for (i = 0; i < chip->n_leds; i++) {
  326. pca963x[i].led_num = i;
  327. pca963x[i].chip = pca963x_chip;
  328. /* Platform data can specify LED names and default triggers */
  329. if (pdata && i < pdata->leds.num_leds) {
  330. if (pdata->leds.leds[i].name)
  331. snprintf(pca963x[i].name,
  332. sizeof(pca963x[i].name), "pca963x:%s",
  333. pdata->leds.leds[i].name);
  334. if (pdata->leds.leds[i].default_trigger)
  335. pca963x[i].led_cdev.default_trigger =
  336. pdata->leds.leds[i].default_trigger;
  337. }
  338. if (!pdata || i >= pdata->leds.num_leds ||
  339. !pdata->leds.leds[i].name)
  340. snprintf(pca963x[i].name, sizeof(pca963x[i].name),
  341. "pca963x:%d:%.2x:%d", client->adapter->nr,
  342. client->addr, i);
  343. pca963x[i].led_cdev.name = pca963x[i].name;
  344. pca963x[i].led_cdev.brightness_set = pca963x_led_set;
  345. if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
  346. pca963x[i].led_cdev.blink_set = pca963x_blink_set;
  347. INIT_WORK(&pca963x[i].work, pca963x_work);
  348. err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
  349. if (err < 0)
  350. goto exit;
  351. }
  352. /* Disable LED all-call address and set normal mode */
  353. i2c_smbus_write_byte_data(client, PCA963X_MODE1, 0x00);
  354. /* Configure output: open-drain or totem pole (push-pull) */
  355. if (pdata && pdata->outdrv == PCA963X_OPEN_DRAIN)
  356. i2c_smbus_write_byte_data(client, PCA963X_MODE2, 0x01);
  357. return 0;
  358. exit:
  359. while (i--) {
  360. led_classdev_unregister(&pca963x[i].led_cdev);
  361. cancel_work_sync(&pca963x[i].work);
  362. }
  363. return err;
  364. }
  365. static int pca963x_remove(struct i2c_client *client)
  366. {
  367. struct pca963x *pca963x = i2c_get_clientdata(client);
  368. int i;
  369. for (i = 0; i < pca963x->chipdef->n_leds; i++) {
  370. led_classdev_unregister(&pca963x->leds[i].led_cdev);
  371. cancel_work_sync(&pca963x->leds[i].work);
  372. }
  373. return 0;
  374. }
  375. static struct i2c_driver pca963x_driver = {
  376. .driver = {
  377. .name = "leds-pca963x",
  378. .owner = THIS_MODULE,
  379. .of_match_table = of_match_ptr(of_pca963x_match),
  380. },
  381. .probe = pca963x_probe,
  382. .remove = pca963x_remove,
  383. .id_table = pca963x_id,
  384. };
  385. module_i2c_driver(pca963x_driver);
  386. MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
  387. MODULE_DESCRIPTION("PCA963X LED driver");
  388. MODULE_LICENSE("GPL v2");