leds-aw2016.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017, 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/i2c.h>
  8. #include <linux/init.h>
  9. #include <linux/leds.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include <linux/regulator/consumer.h>
  14. #include "leds-aw2016.h"
  15. #define AW2016_DRIVER_VERSION "V1.0.3"
  16. /* register address */
  17. #define AW2016_REG_RESET 0x00
  18. #define AW2016_REG_GCR1 0x01
  19. #define AW2016_REG_STATUS 0x02
  20. #define AW2016_REG_PATST 0x03
  21. #define AW2016_REG_GCR2 0x04
  22. #define AW2016_REG_LEDEN 0x30
  23. #define AW2016_REG_LCFG1 0x31
  24. #define AW2016_REG_LCFG2 0x32
  25. #define AW2016_REG_LCFG3 0x33
  26. #define AW2016_REG_PWM1 0x34
  27. #define AW2016_REG_PWM2 0x35
  28. #define AW2016_REG_PWM3 0x36
  29. #define AW2016_REG_LED1T0 0x37
  30. #define AW2016_REG_LED1T1 0x38
  31. #define AW2016_REG_LED1T2 0x39
  32. #define AW2016_REG_LED2T0 0x3A
  33. #define AW2016_REG_LED2T1 0x3B
  34. #define AW2016_REG_LED2T2 0x3C
  35. #define AW2016_REG_LED3T0 0x3D
  36. #define AW2016_REG_LED3T1 0x3E
  37. #define AW2016_REG_LED3T2 0x3F
  38. /* register bits */
  39. #define AW2016_CHIPID 0x09
  40. #define AW2016_CHIP_RESET_MASK 0x55
  41. #define AW2016_CHIP_DISABLE_MASK 0x00
  42. #define AW2016_CHIP_ENABLE_MASK 0x01
  43. #define AW2016_CHARGE_DISABLE_MASK 0x02
  44. #define AW2016_LED_BREATH_MODE_MASK 0x10
  45. #define AW2016_LED_MANUAL_MODE_MASK 0x00
  46. #define AW2016_LED_BREATHE_PWM_MASK 0xFF
  47. #define AW2016_LED_MANUAL_PWM_MASK 0xFF
  48. #define AW2016_LED_FADEIN_MODE_MASK 0x20
  49. #define AW2016_LED_FADEOUT_MODE_MASK 0x40
  50. #define AW2016_CHIP_STANDBY 0x02
  51. #define MAX_RISE_TIME_MS 15
  52. #define MAX_HOLD_TIME_MS 15
  53. #define MAX_FALL_TIME_MS 15
  54. #define MAX_OFF_TIME_MS 15
  55. /* aw2016 register read/write access*/
  56. #define REG_NONE_ACCESS 0
  57. #define REG_RD_ACCESS (1 << 0)
  58. #define REG_WR_ACCESS (1 << 1)
  59. #define AW2016_REG_MAX 0x7F
  60. const unsigned char aw2016_reg_access[AW2016_REG_MAX] = {
  61. [AW2016_REG_RESET] = REG_RD_ACCESS | REG_WR_ACCESS,
  62. [AW2016_REG_GCR1] = REG_RD_ACCESS | REG_WR_ACCESS,
  63. [AW2016_REG_STATUS] = REG_RD_ACCESS,
  64. [AW2016_REG_PATST] = REG_RD_ACCESS,
  65. [AW2016_REG_GCR2] = REG_RD_ACCESS | REG_WR_ACCESS,
  66. [AW2016_REG_LEDEN] = REG_RD_ACCESS | REG_WR_ACCESS,
  67. [AW2016_REG_LCFG1] = REG_RD_ACCESS | REG_WR_ACCESS,
  68. [AW2016_REG_LCFG2] = REG_RD_ACCESS | REG_WR_ACCESS,
  69. [AW2016_REG_LCFG3] = REG_RD_ACCESS | REG_WR_ACCESS,
  70. [AW2016_REG_PWM1] = REG_RD_ACCESS | REG_WR_ACCESS,
  71. [AW2016_REG_PWM2] = REG_RD_ACCESS | REG_WR_ACCESS,
  72. [AW2016_REG_PWM3] = REG_RD_ACCESS | REG_WR_ACCESS,
  73. [AW2016_REG_LED1T0] = REG_RD_ACCESS | REG_WR_ACCESS,
  74. [AW2016_REG_LED1T1] = REG_RD_ACCESS | REG_WR_ACCESS,
  75. [AW2016_REG_LED1T2] = REG_RD_ACCESS | REG_WR_ACCESS,
  76. [AW2016_REG_LED2T0] = REG_RD_ACCESS | REG_WR_ACCESS,
  77. [AW2016_REG_LED2T1] = REG_RD_ACCESS | REG_WR_ACCESS,
  78. [AW2016_REG_LED2T2] = REG_RD_ACCESS | REG_WR_ACCESS,
  79. [AW2016_REG_LED3T0] = REG_RD_ACCESS | REG_WR_ACCESS,
  80. [AW2016_REG_LED3T1] = REG_RD_ACCESS | REG_WR_ACCESS,
  81. [AW2016_REG_LED3T2] = REG_RD_ACCESS | REG_WR_ACCESS,
  82. };
  83. struct aw2016_led {
  84. struct i2c_client *client;
  85. struct led_classdev cdev;
  86. struct aw2016_platform_data *pdata;
  87. struct work_struct brightness_work;
  88. struct work_struct blink_work;
  89. struct mutex lock;
  90. int num_leds;
  91. int id;
  92. int blinking;
  93. };
  94. static int aw2016_write(struct aw2016_led *led, u8 reg, u8 val)
  95. {
  96. int ret = -EINVAL, retry_times = 0;
  97. do {
  98. ret = i2c_smbus_write_byte_data(led->client, reg, val);
  99. retry_times++;
  100. if (retry_times == 5)
  101. break;
  102. } while (ret < 0);
  103. return ret;
  104. }
  105. static int aw2016_read(struct aw2016_led *led, u8 reg, u8 *val)
  106. {
  107. int ret = -EINVAL, retry_times = 0;
  108. do {
  109. ret = i2c_smbus_read_byte_data(led->client, reg);
  110. retry_times++;
  111. if (retry_times == 5)
  112. break;
  113. } while (ret < 0);
  114. if (ret < 0)
  115. return ret;
  116. *val = ret;
  117. return 0;
  118. }
  119. static void aw2016_soft_reset(struct aw2016_led *led)
  120. {
  121. aw2016_write(led, AW2016_REG_RESET, AW2016_CHIP_RESET_MASK);
  122. usleep_range(5000, 6000);
  123. }
  124. static void aw2016_brightness_work(struct work_struct *work)
  125. {
  126. struct aw2016_led *led =
  127. container_of(work, struct aw2016_led, brightness_work);
  128. u8 val;
  129. mutex_lock(&led->pdata->led->lock);
  130. /* enable aw2016 if disabled */
  131. aw2016_read(led, AW2016_REG_GCR1, &val);
  132. if (!(val & AW2016_CHIP_ENABLE_MASK)) {
  133. aw2016_write(led, AW2016_REG_GCR1,
  134. AW2016_CHARGE_DISABLE_MASK |
  135. AW2016_CHIP_ENABLE_MASK);
  136. usleep_range(2000, 3000);
  137. }
  138. if (led->cdev.brightness > 0) {
  139. if (led->cdev.brightness > led->cdev.max_brightness)
  140. led->cdev.brightness = led->cdev.max_brightness;
  141. aw2016_write(led, AW2016_REG_GCR2, led->pdata->imax);
  142. aw2016_write(led, AW2016_REG_LCFG1 + led->id,
  143. (AW2016_LED_MANUAL_MODE_MASK |
  144. led->pdata->led_current));
  145. aw2016_write(led, AW2016_REG_PWM1 + led->id,
  146. led->cdev.brightness);
  147. aw2016_read(led, AW2016_REG_LEDEN, &val);
  148. aw2016_write(led, AW2016_REG_LEDEN, val | (1 << led->id));
  149. } else {
  150. aw2016_read(led, AW2016_REG_LEDEN, &val);
  151. aw2016_write(led, AW2016_REG_LEDEN, val & (~(1 << led->id)));
  152. }
  153. /*
  154. * If value in AW2016_REG_LEDEN is 0, it means the RGB leds are
  155. * all off. So we need to power it off.
  156. */
  157. aw2016_read(led, AW2016_REG_LEDEN, &val);
  158. if (val == 0) {
  159. aw2016_write(led, AW2016_REG_GCR1,
  160. AW2016_CHARGE_DISABLE_MASK |
  161. AW2016_CHIP_DISABLE_MASK);
  162. mutex_unlock(&led->pdata->led->lock);
  163. return;
  164. }
  165. mutex_unlock(&led->pdata->led->lock);
  166. }
  167. static void aw2016_blink_work(struct work_struct *work)
  168. {
  169. struct aw2016_led *led =
  170. container_of(work, struct aw2016_led, blink_work);
  171. u8 val;
  172. mutex_lock(&led->pdata->led->lock);
  173. /* enable aw2016 if disabled */
  174. aw2016_read(led, AW2016_REG_GCR1, &val);
  175. if (!(val & AW2016_CHIP_ENABLE_MASK)) {
  176. aw2016_write(led, AW2016_REG_GCR1,
  177. AW2016_CHARGE_DISABLE_MASK |
  178. AW2016_CHIP_ENABLE_MASK);
  179. usleep_range(2000, 3000);
  180. }
  181. led->cdev.brightness = led->blinking ? led->cdev.max_brightness : 0;
  182. if (led->blinking > 0) {
  183. aw2016_write(led, AW2016_REG_GCR2, led->pdata->imax);
  184. aw2016_write(led, AW2016_REG_PWM1 + led->id,
  185. led->cdev.brightness);
  186. aw2016_write(led, AW2016_REG_LED1T0 + led->id * 3,
  187. (led->pdata->rise_time_ms << 4 |
  188. led->pdata->hold_time_ms));
  189. aw2016_write(led, AW2016_REG_LED1T1 + led->id * 3,
  190. (led->pdata->fall_time_ms << 4 |
  191. led->pdata->off_time_ms));
  192. aw2016_write(led, AW2016_REG_LCFG1 + led->id,
  193. (AW2016_LED_BREATH_MODE_MASK |
  194. led->pdata->led_current));
  195. aw2016_read(led, AW2016_REG_LEDEN, &val);
  196. aw2016_write(led, AW2016_REG_LEDEN, val | (1 << led->id));
  197. } else {
  198. aw2016_read(led, AW2016_REG_LEDEN, &val);
  199. aw2016_write(led, AW2016_REG_LEDEN, val & (~(1 << led->id)));
  200. }
  201. /*
  202. * If value in AW2016_REG_LEDEN is 0, it means the RGB leds are
  203. * all off. So we need to power it off.
  204. */
  205. aw2016_read(led, AW2016_REG_LEDEN, &val);
  206. if (val == 0) {
  207. aw2016_write(led, AW2016_REG_GCR1,
  208. AW2016_CHARGE_DISABLE_MASK |
  209. AW2016_CHIP_DISABLE_MASK);
  210. }
  211. mutex_unlock(&led->pdata->led->lock);
  212. }
  213. static enum led_brightness aw2016_get_brightness(struct led_classdev *cdev)
  214. {
  215. return cdev->brightness;
  216. }
  217. static void aw2016_set_brightness(struct led_classdev *cdev,
  218. enum led_brightness brightness)
  219. {
  220. struct aw2016_led *led = container_of(cdev, struct aw2016_led, cdev);
  221. led->cdev.brightness = brightness;
  222. schedule_work(&led->brightness_work);
  223. }
  224. static ssize_t breath_show(struct device *dev,
  225. struct device_attribute *attr, char *buf)
  226. {
  227. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  228. struct aw2016_led *led = container_of(led_cdev, struct aw2016_led, cdev);
  229. return led->blinking;
  230. }
  231. static ssize_t breath_store(struct device *dev,
  232. struct device_attribute *attr,
  233. const char *buf, size_t len)
  234. {
  235. unsigned long blinking;
  236. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  237. struct aw2016_led *led =
  238. container_of(led_cdev, struct aw2016_led, cdev);
  239. ssize_t ret = -EINVAL;
  240. ret = kstrtoul(buf, 10, &blinking);
  241. if (ret)
  242. return ret;
  243. led->blinking = (int)blinking;
  244. schedule_work(&led->blink_work);
  245. return len;
  246. }
  247. static ssize_t led_time_show(struct device *dev,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  251. struct aw2016_led *led =
  252. container_of(led_cdev, struct aw2016_led, cdev);
  253. return scnprintf(buf, PAGE_SIZE, "%d %d %d %d\n",
  254. led->pdata->rise_time_ms, led->pdata->hold_time_ms,
  255. led->pdata->fall_time_ms, led->pdata->off_time_ms);
  256. }
  257. static ssize_t led_time_store(struct device *dev,
  258. struct device_attribute *attr,
  259. const char *buf, size_t len)
  260. {
  261. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  262. struct aw2016_led *led =
  263. container_of(led_cdev, struct aw2016_led, cdev);
  264. int rc, rise_time_ms, hold_time_ms, fall_time_ms, off_time_ms;
  265. rc = sscanf(buf, "%d %d %d %d", &rise_time_ms, &hold_time_ms,
  266. &fall_time_ms, &off_time_ms);
  267. mutex_lock(&led->pdata->led->lock);
  268. led->pdata->rise_time_ms = (rise_time_ms > MAX_RISE_TIME_MS) ?
  269. MAX_RISE_TIME_MS :
  270. rise_time_ms;
  271. led->pdata->hold_time_ms = (hold_time_ms > MAX_HOLD_TIME_MS) ?
  272. MAX_HOLD_TIME_MS :
  273. hold_time_ms;
  274. led->pdata->fall_time_ms = (fall_time_ms > MAX_FALL_TIME_MS) ?
  275. MAX_FALL_TIME_MS :
  276. fall_time_ms;
  277. led->pdata->off_time_ms =
  278. (off_time_ms > MAX_OFF_TIME_MS) ? MAX_OFF_TIME_MS : off_time_ms;
  279. led->blinking = 1;
  280. mutex_unlock(&led->pdata->led->lock);
  281. schedule_work(&led->blink_work);
  282. return len;
  283. }
  284. static ssize_t reg_show(struct device *dev,
  285. struct device_attribute *attr, char *buf)
  286. {
  287. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  288. struct aw2016_led *led =
  289. container_of(led_cdev, struct aw2016_led, cdev);
  290. unsigned char i, reg_val;
  291. ssize_t len = 0;
  292. for (i = 0; i < AW2016_REG_MAX; i++) {
  293. if (!(aw2016_reg_access[i] & REG_RD_ACCESS))
  294. continue;
  295. aw2016_read(led, i, &reg_val);
  296. len += scnprintf(buf + len, PAGE_SIZE - len,
  297. "reg:0x%02x=0x%02x\n", i, reg_val);
  298. }
  299. return len;
  300. }
  301. static ssize_t reg_store(struct device *dev,
  302. struct device_attribute *attr, const char *buf,
  303. size_t len)
  304. {
  305. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  306. struct aw2016_led *led =
  307. container_of(led_cdev, struct aw2016_led, cdev);
  308. unsigned int databuf[2];
  309. if (sscanf(buf, "%x %x", &databuf[0], &databuf[1]) == 2) {
  310. aw2016_write(led, (unsigned char)databuf[0],
  311. (unsigned char)databuf[1]);
  312. }
  313. return len;
  314. }
  315. static DEVICE_ATTR_RW(breath);
  316. static DEVICE_ATTR_RW(led_time);
  317. static DEVICE_ATTR_RW(reg);
  318. static struct attribute *aw2016_led_attributes[] = {
  319. &dev_attr_breath.attr,
  320. &dev_attr_led_time.attr,
  321. &dev_attr_reg.attr,
  322. NULL,
  323. };
  324. static struct attribute_group aw2016_led_attr_group = {
  325. .attrs = aw2016_led_attributes
  326. };
  327. static int aw2016_check_chipid(struct aw2016_led *led)
  328. {
  329. u8 val;
  330. u8 cnt;
  331. for (cnt = 5; cnt > 0; cnt--) {
  332. aw2016_read(led, AW2016_REG_RESET, &val);
  333. dev_notice(&led->client->dev, "aw2016 chip id %0x\n", val);
  334. if (val == AW2016_CHIPID)
  335. return 0;
  336. }
  337. return -EINVAL;
  338. }
  339. static int aw2016_led_err_handle(struct aw2016_led *led_array, int parsed_leds)
  340. {
  341. int i;
  342. /*
  343. * If probe fails, cannot free resource of all LEDs, only free
  344. * resources of LEDs which have allocated these resource really.
  345. */
  346. for (i = 0; i < parsed_leds; i++) {
  347. sysfs_remove_group(&led_array[i].cdev.dev->kobj,
  348. &aw2016_led_attr_group);
  349. led_classdev_unregister(&led_array[i].cdev);
  350. cancel_work_sync(&led_array[i].brightness_work);
  351. cancel_work_sync(&led_array[i].blink_work);
  352. led_array[i].pdata = NULL;
  353. }
  354. return i;
  355. }
  356. static int aw2016_led_parse_child_node(struct aw2016_led *led_array,
  357. struct device_node *node)
  358. {
  359. struct aw2016_led *led;
  360. struct device_node *temp;
  361. struct aw2016_platform_data *pdata;
  362. int rc = 0, parsed_leds = 0;
  363. for_each_child_of_node(node, temp) {
  364. led = &led_array[parsed_leds];
  365. led->client = led_array->client;
  366. pdata = devm_kzalloc(&led->client->dev,
  367. sizeof(struct aw2016_platform_data),
  368. GFP_KERNEL);
  369. if (!pdata) {
  370. dev_err(&led->client->dev,
  371. "Failed to allocate memory\n");
  372. goto free_err;
  373. }
  374. pdata->led = led_array;
  375. led->pdata = pdata;
  376. rc = of_property_read_string(temp, "awinic,name",
  377. &led->cdev.name);
  378. if (rc < 0) {
  379. dev_err(&led->client->dev,
  380. "Failure reading led name, rc = %d\n", rc);
  381. goto free_pdata;
  382. }
  383. rc = of_property_read_u32(temp, "awinic,id", &led->id);
  384. if (rc < 0) {
  385. dev_err(&led->client->dev,
  386. "Failure reading id, rc = %d\n", rc);
  387. goto free_pdata;
  388. }
  389. rc = of_property_read_u32(temp, "awinic,imax",
  390. &led->pdata->imax);
  391. if (rc < 0) {
  392. dev_err(&led->client->dev,
  393. "Failure reading imax, rc = %d\n", rc);
  394. goto free_pdata;
  395. }
  396. rc = of_property_read_u32(temp, "awinic,led-current",
  397. &led->pdata->led_current);
  398. if (rc < 0) {
  399. dev_err(&led->client->dev,
  400. "Failure reading led-current, rc = %d\n", rc);
  401. goto free_pdata;
  402. }
  403. rc = of_property_read_u32(temp, "awinic,max-brightness",
  404. &led->cdev.max_brightness);
  405. if (rc < 0) {
  406. dev_err(&led->client->dev,
  407. "Failure reading max-brightness, rc = %d\n",
  408. rc);
  409. goto free_pdata;
  410. }
  411. rc = of_property_read_u32(temp, "awinic,rise-time-ms",
  412. &led->pdata->rise_time_ms);
  413. if (rc < 0) {
  414. dev_err(&led->client->dev,
  415. "Failure reading rise-time-ms, rc = %d\n", rc);
  416. goto free_pdata;
  417. }
  418. rc = of_property_read_u32(temp, "awinic,hold-time-ms",
  419. &led->pdata->hold_time_ms);
  420. if (rc < 0) {
  421. dev_err(&led->client->dev,
  422. "Failure reading hold-time-ms, rc = %d\n", rc);
  423. goto free_pdata;
  424. }
  425. rc = of_property_read_u32(temp, "awinic,fall-time-ms",
  426. &led->pdata->fall_time_ms);
  427. if (rc < 0) {
  428. dev_err(&led->client->dev,
  429. "Failure reading fall-time-ms, rc = %d\n", rc);
  430. goto free_pdata;
  431. }
  432. rc = of_property_read_u32(temp, "awinic,off-time-ms",
  433. &led->pdata->off_time_ms);
  434. if (rc < 0) {
  435. dev_err(&led->client->dev,
  436. "Failure reading off-time-ms, rc = %d\n", rc);
  437. goto free_pdata;
  438. }
  439. INIT_WORK(&led->brightness_work, aw2016_brightness_work);
  440. INIT_WORK(&led->blink_work, aw2016_blink_work);
  441. led->cdev.brightness_set = aw2016_set_brightness;
  442. led->cdev.brightness_get = aw2016_get_brightness;
  443. rc = led_classdev_register(&led->client->dev, &led->cdev);
  444. if (rc) {
  445. dev_err(&led->client->dev,
  446. "unable to register led %d,rc=%d\n", led->id,
  447. rc);
  448. goto free_pdata;
  449. }
  450. rc = sysfs_create_group(&led->cdev.dev->kobj,
  451. &aw2016_led_attr_group);
  452. if (rc) {
  453. dev_err(&led->client->dev, "led sysfs rc: %d\n", rc);
  454. goto free_class;
  455. }
  456. parsed_leds++;
  457. }
  458. return 0;
  459. free_class:
  460. aw2016_led_err_handle(led_array, parsed_leds);
  461. led_classdev_unregister(&led_array[parsed_leds].cdev);
  462. cancel_work_sync(&led_array[parsed_leds].brightness_work);
  463. cancel_work_sync(&led_array[parsed_leds].blink_work);
  464. led_array[parsed_leds].pdata = NULL;
  465. return rc;
  466. free_pdata:
  467. aw2016_led_err_handle(led_array, parsed_leds);
  468. return rc;
  469. free_err:
  470. aw2016_led_err_handle(led_array, parsed_leds);
  471. return rc;
  472. }
  473. static int aw2016_led_probe(struct i2c_client *client,
  474. const struct i2c_device_id *id)
  475. {
  476. struct aw2016_led *led_array;
  477. struct device_node *node;
  478. int ret = -EINVAL, num_leds = 0;
  479. node = client->dev.of_node;
  480. if (node == NULL)
  481. return -EINVAL;
  482. num_leds = of_get_child_count(node);
  483. if (!num_leds)
  484. return -EINVAL;
  485. led_array = devm_kzalloc(&client->dev,
  486. (sizeof(struct aw2016_led) * num_leds),
  487. GFP_KERNEL);
  488. if (!led_array)
  489. return -ENOMEM;
  490. led_array->client = client;
  491. led_array->num_leds = num_leds;
  492. mutex_init(&led_array->lock);
  493. ret = aw2016_led_parse_child_node(led_array, node);
  494. if (ret) {
  495. dev_err(&client->dev, "parsed node error\n");
  496. goto free_led_arry;
  497. }
  498. i2c_set_clientdata(client, led_array);
  499. ret = aw2016_check_chipid(led_array);
  500. if (ret) {
  501. dev_err(&client->dev, "Check chip id error\n");
  502. goto fail_parsed_node;
  503. }
  504. /* soft rst */
  505. aw2016_soft_reset(led_array);
  506. return 0;
  507. fail_parsed_node:
  508. aw2016_led_err_handle(led_array, num_leds);
  509. free_led_arry:
  510. mutex_destroy(&led_array->lock);
  511. led_array = NULL;
  512. return ret;
  513. }
  514. static void aw2016_led_remove(struct i2c_client *client)
  515. {
  516. struct aw2016_led *led_array = i2c_get_clientdata(client);
  517. int i, parsed_leds = led_array->num_leds;
  518. for (i = 0; i < parsed_leds; i++) {
  519. sysfs_remove_group(&led_array[i].cdev.dev->kobj,
  520. &aw2016_led_attr_group);
  521. led_classdev_unregister(&led_array[i].cdev);
  522. cancel_work_sync(&led_array[i].brightness_work);
  523. cancel_work_sync(&led_array[i].blink_work);
  524. led_array[i].pdata = NULL;
  525. }
  526. mutex_destroy(&led_array->lock);
  527. led_array = NULL;
  528. }
  529. static void aw2016_led_shutdown(struct i2c_client *client)
  530. {
  531. struct aw2016_led *led = i2c_get_clientdata(client);
  532. aw2016_write(led, AW2016_REG_GCR1, AW2016_CHIP_STANDBY);
  533. }
  534. static const struct i2c_device_id aw2016_led_id[] = {
  535. { "aw2016_led", 0 },
  536. {},
  537. };
  538. MODULE_DEVICE_TABLE(i2c, aw2016_led_id);
  539. static const struct of_device_id aw2016_match_table[] = {
  540. {
  541. .compatible = "awinic,aw2016_led",
  542. },
  543. {},
  544. };
  545. static struct i2c_driver aw2016_led_driver = {
  546. .probe = aw2016_led_probe,
  547. .remove = aw2016_led_remove,
  548. .shutdown = aw2016_led_shutdown,
  549. .driver = {
  550. .name = "aw2016_led",
  551. .of_match_table = of_match_ptr(aw2016_match_table),
  552. },
  553. .id_table = aw2016_led_id,
  554. };
  555. static int __init aw2016_led_init(void)
  556. {
  557. pr_info("%s: driver version: %s\n", __func__, AW2016_DRIVER_VERSION);
  558. return i2c_add_driver(&aw2016_led_driver);
  559. }
  560. module_init(aw2016_led_init);
  561. static void __exit aw2016_led_exit(void)
  562. {
  563. i2c_del_driver(&aw2016_led_driver);
  564. }
  565. module_exit(aw2016_led_exit);
  566. MODULE_AUTHOR("<[email protected]>");
  567. MODULE_DESCRIPTION("AWINIC AW2016 LED driver");
  568. MODULE_LICENSE("GPL v2");