leds-pca9633.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2011 bct electronic GmbH
  3. *
  4. * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
  5. *
  6. * Based on leds-pca955x.c
  7. *
  8. * This file is subject to the terms and conditions of version 2 of
  9. * the GNU General Public License. See the file COPYING in the main
  10. * directory of this archive for more details.
  11. *
  12. * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
  13. *
  14. * Note that hardware blinking violates the leds infrastructure driver
  15. * interface since the hardware only supports blinking all LEDs with the
  16. * same delay_on/delay_off rates. That is, only the LEDs that are set to
  17. * blink will actually blink but all LEDs that are set to blink will blink
  18. * in identical fashion. The delay_on/delay_off values of the last LED
  19. * that is set to blink will be used for all of the blinking LEDs.
  20. * Hardware blinking is disabled by default but can be enabled by setting
  21. * the 'blink_type' member in the platform_data struct to 'PCA9633_HW_BLINK'
  22. * or by adding the 'nxp,hw-blink' property to the DTS.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/delay.h>
  26. #include <linux/string.h>
  27. #include <linux/ctype.h>
  28. #include <linux/leds.h>
  29. #include <linux/err.h>
  30. #include <linux/i2c.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/slab.h>
  33. #include <linux/of.h>
  34. #include <linux/platform_data/leds-pca9633.h>
  35. /* LED select registers determine the source that drives LED outputs */
  36. #define PCA9633_LED_OFF 0x0 /* LED driver off */
  37. #define PCA9633_LED_ON 0x1 /* LED driver on */
  38. #define PCA9633_LED_PWM 0x2 /* Controlled through PWM */
  39. #define PCA9633_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
  40. #define PCA9633_MODE2_DMBLNK 0x20 /* Enable blinking */
  41. #define PCA9633_MODE1 0x00
  42. #define PCA9633_MODE2 0x01
  43. #define PCA9633_PWM_BASE 0x02
  44. #define PCA9633_GRPPWM 0x06
  45. #define PCA9633_GRPFREQ 0x07
  46. #define PCA9633_LEDOUT 0x08
  47. /* Total blink period in milliseconds */
  48. #define PCA9632_BLINK_PERIOD_MIN 42
  49. #define PCA9632_BLINK_PERIOD_MAX 10667
  50. static const struct i2c_device_id pca9633_id[] = {
  51. { "pca9633", 0 },
  52. { }
  53. };
  54. MODULE_DEVICE_TABLE(i2c, pca9633_id);
  55. enum pca9633_cmd {
  56. BRIGHTNESS_SET,
  57. BLINK_SET,
  58. };
  59. struct pca9633_led {
  60. struct i2c_client *client;
  61. struct work_struct work;
  62. enum led_brightness brightness;
  63. struct led_classdev led_cdev;
  64. int led_num; /* 0 .. 3 potentially */
  65. enum pca9633_cmd cmd;
  66. char name[32];
  67. u8 gdc;
  68. u8 gfrq;
  69. };
  70. static void pca9633_brightness_work(struct pca9633_led *pca9633)
  71. {
  72. u8 ledout = i2c_smbus_read_byte_data(pca9633->client, PCA9633_LEDOUT);
  73. int shift = 2 * pca9633->led_num;
  74. u8 mask = 0x3 << shift;
  75. switch (pca9633->brightness) {
  76. case LED_FULL:
  77. i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT,
  78. (ledout & ~mask) | (PCA9633_LED_ON << shift));
  79. break;
  80. case LED_OFF:
  81. i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT,
  82. ledout & ~mask);
  83. break;
  84. default:
  85. i2c_smbus_write_byte_data(pca9633->client,
  86. PCA9633_PWM_BASE + pca9633->led_num,
  87. pca9633->brightness);
  88. i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT,
  89. (ledout & ~mask) | (PCA9633_LED_PWM << shift));
  90. break;
  91. }
  92. }
  93. static void pca9633_blink_work(struct pca9633_led *pca9633)
  94. {
  95. u8 ledout = i2c_smbus_read_byte_data(pca9633->client, PCA9633_LEDOUT);
  96. u8 mode2 = i2c_smbus_read_byte_data(pca9633->client, PCA9633_MODE2);
  97. int shift = 2 * pca9633->led_num;
  98. u8 mask = 0x3 << shift;
  99. i2c_smbus_write_byte_data(pca9633->client, PCA9633_GRPPWM,
  100. pca9633->gdc);
  101. i2c_smbus_write_byte_data(pca9633->client, PCA9633_GRPFREQ,
  102. pca9633->gfrq);
  103. if (!(mode2 & PCA9633_MODE2_DMBLNK))
  104. i2c_smbus_write_byte_data(pca9633->client, PCA9633_MODE2,
  105. mode2 | PCA9633_MODE2_DMBLNK);
  106. if ((ledout & mask) != (PCA9633_LED_GRP_PWM << shift))
  107. i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT,
  108. (ledout & ~mask) | (PCA9633_LED_GRP_PWM << shift));
  109. }
  110. static void pca9633_work(struct work_struct *work)
  111. {
  112. struct pca9633_led *pca9633 = container_of(work,
  113. struct pca9633_led, work);
  114. switch (pca9633->cmd) {
  115. case BRIGHTNESS_SET:
  116. pca9633_brightness_work(pca9633);
  117. break;
  118. case BLINK_SET:
  119. pca9633_blink_work(pca9633);
  120. break;
  121. }
  122. }
  123. static void pca9633_led_set(struct led_classdev *led_cdev,
  124. enum led_brightness value)
  125. {
  126. struct pca9633_led *pca9633;
  127. pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev);
  128. pca9633->cmd = BRIGHTNESS_SET;
  129. pca9633->brightness = value;
  130. /*
  131. * Must use workqueue for the actual I/O since I2C operations
  132. * can sleep.
  133. */
  134. schedule_work(&pca9633->work);
  135. }
  136. static int pca9633_blink_set(struct led_classdev *led_cdev,
  137. unsigned long *delay_on, unsigned long *delay_off)
  138. {
  139. struct pca9633_led *pca9633;
  140. unsigned long time_on, time_off, period;
  141. u8 gdc, gfrq;
  142. pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev);
  143. time_on = *delay_on;
  144. time_off = *delay_off;
  145. /* If both zero, pick reasonable defaults of 500ms each */
  146. if (!time_on && !time_off) {
  147. time_on = 500;
  148. time_off = 500;
  149. }
  150. period = time_on + time_off;
  151. /* If period not supported by hardware, default to someting sane. */
  152. if ((period < PCA9632_BLINK_PERIOD_MIN) ||
  153. (period > PCA9632_BLINK_PERIOD_MAX)) {
  154. time_on = 500;
  155. time_off = 500;
  156. period = time_on + time_off;
  157. }
  158. /*
  159. * From manual: duty cycle = (GDC / 256) ->
  160. * (time_on / period) = (GDC / 256) ->
  161. * GDC = ((time_on * 256) / period)
  162. */
  163. gdc = (time_on * 256) / period;
  164. /*
  165. * From manual: period = ((GFRQ + 1) / 24) in seconds.
  166. * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
  167. * GFRQ = ((period * 24 / 1000) - 1)
  168. */
  169. gfrq = (period * 24 / 1000) - 1;
  170. pca9633->cmd = BLINK_SET;
  171. pca9633->gdc = gdc;
  172. pca9633->gfrq = gfrq;
  173. /*
  174. * Must use workqueue for the actual I/O since I2C operations
  175. * can sleep.
  176. */
  177. schedule_work(&pca9633->work);
  178. *delay_on = time_on;
  179. *delay_off = time_off;
  180. return 0;
  181. }
  182. #if IS_ENABLED(CONFIG_OF)
  183. static struct pca9633_platform_data *
  184. pca9633_dt_init(struct i2c_client *client)
  185. {
  186. struct device_node *np = client->dev.of_node, *child;
  187. struct pca9633_platform_data *pdata;
  188. struct led_info *pca9633_leds;
  189. int count;
  190. count = of_get_child_count(np);
  191. if (!count || count > 4)
  192. return ERR_PTR(-ENODEV);
  193. pca9633_leds = devm_kzalloc(&client->dev,
  194. sizeof(struct led_info) * count, GFP_KERNEL);
  195. if (!pca9633_leds)
  196. return ERR_PTR(-ENOMEM);
  197. for_each_child_of_node(np, child) {
  198. struct led_info led;
  199. u32 reg;
  200. int res;
  201. led.name =
  202. of_get_property(child, "label", NULL) ? : child->name;
  203. led.default_trigger =
  204. of_get_property(child, "linux,default-trigger", NULL);
  205. res = of_property_read_u32(child, "reg", &reg);
  206. if (res != 0)
  207. continue;
  208. pca9633_leds[reg] = led;
  209. }
  210. pdata = devm_kzalloc(&client->dev,
  211. sizeof(struct pca9633_platform_data), GFP_KERNEL);
  212. if (!pdata)
  213. return ERR_PTR(-ENOMEM);
  214. pdata->leds.leds = pca9633_leds;
  215. pdata->leds.num_leds = count;
  216. /* default to open-drain unless totem pole (push-pull) is specified */
  217. if (of_property_read_bool(np, "nxp,totem-pole"))
  218. pdata->outdrv = PCA9633_TOTEM_POLE;
  219. else
  220. pdata->outdrv = PCA9633_OPEN_DRAIN;
  221. /* default to software blinking unless hardware blinking is specified */
  222. if (of_property_read_bool(np, "nxp,hw-blink"))
  223. pdata->blink_type = PCA9633_HW_BLINK;
  224. else
  225. pdata->blink_type = PCA9633_SW_BLINK;
  226. return pdata;
  227. }
  228. static const struct of_device_id of_pca9633_match[] = {
  229. { .compatible = "nxp,pca963x", },
  230. {},
  231. };
  232. #else
  233. static struct pca9633_platform_data *
  234. pca9633_dt_init(struct i2c_client *client)
  235. {
  236. return ERR_PTR(-ENODEV);
  237. }
  238. #endif
  239. static int pca9633_probe(struct i2c_client *client,
  240. const struct i2c_device_id *id)
  241. {
  242. struct pca9633_led *pca9633;
  243. struct pca9633_platform_data *pdata;
  244. int i, err;
  245. pdata = client->dev.platform_data;
  246. if (!pdata) {
  247. pdata = pca9633_dt_init(client);
  248. if (IS_ERR(pdata)) {
  249. dev_warn(&client->dev, "could not parse configuration\n");
  250. pdata = NULL;
  251. }
  252. }
  253. if (pdata) {
  254. if (pdata->leds.num_leds <= 0 || pdata->leds.num_leds > 4) {
  255. dev_err(&client->dev, "board info must claim at most 4 LEDs");
  256. return -EINVAL;
  257. }
  258. }
  259. pca9633 = devm_kzalloc(&client->dev, 4 * sizeof(*pca9633), GFP_KERNEL);
  260. if (!pca9633)
  261. return -ENOMEM;
  262. i2c_set_clientdata(client, pca9633);
  263. for (i = 0; i < 4; i++) {
  264. pca9633[i].client = client;
  265. pca9633[i].led_num = i;
  266. /* Platform data can specify LED names and default triggers */
  267. if (pdata && i < pdata->leds.num_leds) {
  268. if (pdata->leds.leds[i].name)
  269. snprintf(pca9633[i].name,
  270. sizeof(pca9633[i].name), "pca9633:%s",
  271. pdata->leds.leds[i].name);
  272. if (pdata->leds.leds[i].default_trigger)
  273. pca9633[i].led_cdev.default_trigger =
  274. pdata->leds.leds[i].default_trigger;
  275. } else {
  276. snprintf(pca9633[i].name, sizeof(pca9633[i].name),
  277. "pca9633:%d", i);
  278. }
  279. pca9633[i].led_cdev.name = pca9633[i].name;
  280. pca9633[i].led_cdev.brightness_set = pca9633_led_set;
  281. if (pdata && pdata->blink_type == PCA9633_HW_BLINK)
  282. pca9633[i].led_cdev.blink_set = pca9633_blink_set;
  283. INIT_WORK(&pca9633[i].work, pca9633_work);
  284. err = led_classdev_register(&client->dev, &pca9633[i].led_cdev);
  285. if (err < 0)
  286. goto exit;
  287. }
  288. /* Disable LED all-call address and set normal mode */
  289. i2c_smbus_write_byte_data(client, PCA9633_MODE1, 0x00);
  290. /* Configure output: open-drain or totem pole (push-pull) */
  291. if (pdata && pdata->outdrv == PCA9633_OPEN_DRAIN)
  292. i2c_smbus_write_byte_data(client, PCA9633_MODE2, 0x01);
  293. /* Turn off LEDs */
  294. i2c_smbus_write_byte_data(client, PCA9633_LEDOUT, 0x00);
  295. return 0;
  296. exit:
  297. while (i--) {
  298. led_classdev_unregister(&pca9633[i].led_cdev);
  299. cancel_work_sync(&pca9633[i].work);
  300. }
  301. return err;
  302. }
  303. static int pca9633_remove(struct i2c_client *client)
  304. {
  305. struct pca9633_led *pca9633 = i2c_get_clientdata(client);
  306. int i;
  307. for (i = 0; i < 4; i++) {
  308. led_classdev_unregister(&pca9633[i].led_cdev);
  309. cancel_work_sync(&pca9633[i].work);
  310. }
  311. return 0;
  312. }
  313. static struct i2c_driver pca9633_driver = {
  314. .driver = {
  315. .name = "leds-pca9633",
  316. .owner = THIS_MODULE,
  317. .of_match_table = of_match_ptr(of_pca9633_match),
  318. },
  319. .probe = pca9633_probe,
  320. .remove = pca9633_remove,
  321. .id_table = pca9633_id,
  322. };
  323. module_i2c_driver(pca9633_driver);
  324. MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
  325. MODULE_DESCRIPTION("PCA9633 LED driver");
  326. MODULE_LICENSE("GPL v2");