leds-bd2802.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * leds-bd2802.c - RGB LED Driver
  3. *
  4. * Copyright (C) 2009 Samsung Electronics
  5. * Kim Kyuwon <q1.kim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Datasheet: http://www.rohm.com/products/databook/driver/pdf/bd2802gu-e.pdf
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/gpio.h>
  17. #include <linux/delay.h>
  18. #include <linux/leds.h>
  19. #include <linux/leds-bd2802.h>
  20. #include <linux/slab.h>
  21. #include <linux/pm.h>
  22. #define LED_CTL(rgb2en, rgb1en) ((rgb2en) << 4 | ((rgb1en) << 0))
  23. #define BD2802_LED_OFFSET 0xa
  24. #define BD2802_COLOR_OFFSET 0x3
  25. #define BD2802_REG_CLKSETUP 0x00
  26. #define BD2802_REG_CONTROL 0x01
  27. #define BD2802_REG_HOURSETUP 0x02
  28. #define BD2802_REG_CURRENT1SETUP 0x03
  29. #define BD2802_REG_CURRENT2SETUP 0x04
  30. #define BD2802_REG_WAVEPATTERN 0x05
  31. #define BD2802_CURRENT_032 0x10 /* 3.2mA */
  32. #define BD2802_CURRENT_000 0x00 /* 0.0mA */
  33. #define BD2802_PATTERN_FULL 0x07
  34. #define BD2802_PATTERN_HALF 0x03
  35. enum led_ids {
  36. LED1,
  37. LED2,
  38. LED_NUM,
  39. };
  40. enum led_colors {
  41. RED,
  42. GREEN,
  43. BLUE,
  44. };
  45. enum led_bits {
  46. BD2802_OFF,
  47. BD2802_BLINK,
  48. BD2802_ON,
  49. };
  50. /*
  51. * State '0' : 'off'
  52. * State '1' : 'blink'
  53. * State '2' : 'on'.
  54. */
  55. struct led_state {
  56. unsigned r:2;
  57. unsigned g:2;
  58. unsigned b:2;
  59. };
  60. struct bd2802_led {
  61. struct bd2802_led_platform_data *pdata;
  62. struct i2c_client *client;
  63. struct rw_semaphore rwsem;
  64. struct led_state led[2];
  65. /*
  66. * Making led_classdev as array is not recommended, because array
  67. * members prevent using 'container_of' macro. So repetitive works
  68. * are needed.
  69. */
  70. struct led_classdev cdev_led1r;
  71. struct led_classdev cdev_led1g;
  72. struct led_classdev cdev_led1b;
  73. struct led_classdev cdev_led2r;
  74. struct led_classdev cdev_led2g;
  75. struct led_classdev cdev_led2b;
  76. /*
  77. * Advanced Configuration Function(ADF) mode:
  78. * In ADF mode, user can set registers of BD2802GU directly,
  79. * therefore BD2802GU doesn't enter reset state.
  80. */
  81. int adf_on;
  82. enum led_ids led_id;
  83. enum led_colors color;
  84. enum led_bits state;
  85. /* General attributes of RGB LEDs */
  86. int wave_pattern;
  87. int rgb_current;
  88. };
  89. /*--------------------------------------------------------------*/
  90. /* BD2802GU helper functions */
  91. /*--------------------------------------------------------------*/
  92. static inline int bd2802_is_rgb_off(struct bd2802_led *led, enum led_ids id,
  93. enum led_colors color)
  94. {
  95. switch (color) {
  96. case RED:
  97. return !led->led[id].r;
  98. case GREEN:
  99. return !led->led[id].g;
  100. case BLUE:
  101. return !led->led[id].b;
  102. default:
  103. dev_err(&led->client->dev, "%s: Invalid color\n", __func__);
  104. return -EINVAL;
  105. }
  106. }
  107. static inline int bd2802_is_led_off(struct bd2802_led *led, enum led_ids id)
  108. {
  109. if (led->led[id].r || led->led[id].g || led->led[id].b)
  110. return 0;
  111. return 1;
  112. }
  113. static inline int bd2802_is_all_off(struct bd2802_led *led)
  114. {
  115. int i;
  116. for (i = 0; i < LED_NUM; i++)
  117. if (!bd2802_is_led_off(led, i))
  118. return 0;
  119. return 1;
  120. }
  121. static inline u8 bd2802_get_base_offset(enum led_ids id, enum led_colors color)
  122. {
  123. return id * BD2802_LED_OFFSET + color * BD2802_COLOR_OFFSET;
  124. }
  125. static inline u8 bd2802_get_reg_addr(enum led_ids id, enum led_colors color,
  126. u8 reg_offset)
  127. {
  128. return reg_offset + bd2802_get_base_offset(id, color);
  129. }
  130. /*--------------------------------------------------------------*/
  131. /* BD2802GU core functions */
  132. /*--------------------------------------------------------------*/
  133. static int bd2802_write_byte(struct i2c_client *client, u8 reg, u8 val)
  134. {
  135. int ret = i2c_smbus_write_byte_data(client, reg, val);
  136. if (ret >= 0)
  137. return 0;
  138. dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
  139. __func__, reg, val, ret);
  140. return ret;
  141. }
  142. static void bd2802_update_state(struct bd2802_led *led, enum led_ids id,
  143. enum led_colors color, enum led_bits led_bit)
  144. {
  145. int i;
  146. u8 value;
  147. for (i = 0; i < LED_NUM; i++) {
  148. if (i == id) {
  149. switch (color) {
  150. case RED:
  151. led->led[i].r = led_bit;
  152. break;
  153. case GREEN:
  154. led->led[i].g = led_bit;
  155. break;
  156. case BLUE:
  157. led->led[i].b = led_bit;
  158. break;
  159. default:
  160. dev_err(&led->client->dev,
  161. "%s: Invalid color\n", __func__);
  162. return;
  163. }
  164. }
  165. }
  166. if (led_bit == BD2802_BLINK || led_bit == BD2802_ON)
  167. return;
  168. if (!bd2802_is_led_off(led, id))
  169. return;
  170. if (bd2802_is_all_off(led) && !led->adf_on) {
  171. gpio_set_value(led->pdata->reset_gpio, 0);
  172. return;
  173. }
  174. /*
  175. * In this case, other led is turned on, and current led is turned
  176. * off. So set RGB LED Control register to stop the current RGB LED
  177. */
  178. value = (id == LED1) ? LED_CTL(1, 0) : LED_CTL(0, 1);
  179. bd2802_write_byte(led->client, BD2802_REG_CONTROL, value);
  180. }
  181. static void bd2802_configure(struct bd2802_led *led)
  182. {
  183. struct bd2802_led_platform_data *pdata = led->pdata;
  184. u8 reg;
  185. reg = bd2802_get_reg_addr(LED1, RED, BD2802_REG_HOURSETUP);
  186. bd2802_write_byte(led->client, reg, pdata->rgb_time);
  187. reg = bd2802_get_reg_addr(LED2, RED, BD2802_REG_HOURSETUP);
  188. bd2802_write_byte(led->client, reg, pdata->rgb_time);
  189. }
  190. static void bd2802_reset_cancel(struct bd2802_led *led)
  191. {
  192. gpio_set_value(led->pdata->reset_gpio, 1);
  193. udelay(100);
  194. bd2802_configure(led);
  195. }
  196. static void bd2802_enable(struct bd2802_led *led, enum led_ids id)
  197. {
  198. enum led_ids other_led = (id == LED1) ? LED2 : LED1;
  199. u8 value, other_led_on;
  200. other_led_on = !bd2802_is_led_off(led, other_led);
  201. if (id == LED1)
  202. value = LED_CTL(other_led_on, 1);
  203. else
  204. value = LED_CTL(1 , other_led_on);
  205. bd2802_write_byte(led->client, BD2802_REG_CONTROL, value);
  206. }
  207. static void bd2802_set_on(struct bd2802_led *led, enum led_ids id,
  208. enum led_colors color)
  209. {
  210. u8 reg;
  211. if (bd2802_is_all_off(led) && !led->adf_on)
  212. bd2802_reset_cancel(led);
  213. reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
  214. bd2802_write_byte(led->client, reg, led->rgb_current);
  215. reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
  216. bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
  217. reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN);
  218. bd2802_write_byte(led->client, reg, BD2802_PATTERN_FULL);
  219. bd2802_enable(led, id);
  220. bd2802_update_state(led, id, color, BD2802_ON);
  221. }
  222. static void bd2802_set_blink(struct bd2802_led *led, enum led_ids id,
  223. enum led_colors color)
  224. {
  225. u8 reg;
  226. if (bd2802_is_all_off(led) && !led->adf_on)
  227. bd2802_reset_cancel(led);
  228. reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
  229. bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
  230. reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
  231. bd2802_write_byte(led->client, reg, led->rgb_current);
  232. reg = bd2802_get_reg_addr(id, color, BD2802_REG_WAVEPATTERN);
  233. bd2802_write_byte(led->client, reg, led->wave_pattern);
  234. bd2802_enable(led, id);
  235. bd2802_update_state(led, id, color, BD2802_BLINK);
  236. }
  237. static void bd2802_turn_on(struct bd2802_led *led, enum led_ids id,
  238. enum led_colors color, enum led_bits led_bit)
  239. {
  240. if (led_bit == BD2802_OFF) {
  241. dev_err(&led->client->dev,
  242. "Only 'blink' and 'on' are allowed\n");
  243. return;
  244. }
  245. if (led_bit == BD2802_BLINK)
  246. bd2802_set_blink(led, id, color);
  247. else
  248. bd2802_set_on(led, id, color);
  249. }
  250. static void bd2802_turn_off(struct bd2802_led *led, enum led_ids id,
  251. enum led_colors color)
  252. {
  253. u8 reg;
  254. if (bd2802_is_rgb_off(led, id, color))
  255. return;
  256. reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT1SETUP);
  257. bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
  258. reg = bd2802_get_reg_addr(id, color, BD2802_REG_CURRENT2SETUP);
  259. bd2802_write_byte(led->client, reg, BD2802_CURRENT_000);
  260. bd2802_update_state(led, id, color, BD2802_OFF);
  261. }
  262. #define BD2802_SET_REGISTER(reg_addr, reg_name) \
  263. static ssize_t bd2802_store_reg##reg_addr(struct device *dev, \
  264. struct device_attribute *attr, const char *buf, size_t count) \
  265. { \
  266. struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
  267. unsigned long val; \
  268. int ret; \
  269. if (!count) \
  270. return -EINVAL; \
  271. ret = kstrtoul(buf, 16, &val); \
  272. if (ret) \
  273. return ret; \
  274. down_write(&led->rwsem); \
  275. bd2802_write_byte(led->client, reg_addr, (u8) val); \
  276. up_write(&led->rwsem); \
  277. return count; \
  278. } \
  279. static struct device_attribute bd2802_reg##reg_addr##_attr = { \
  280. .attr = {.name = reg_name, .mode = 0644}, \
  281. .store = bd2802_store_reg##reg_addr, \
  282. };
  283. BD2802_SET_REGISTER(0x00, "0x00");
  284. BD2802_SET_REGISTER(0x01, "0x01");
  285. BD2802_SET_REGISTER(0x02, "0x02");
  286. BD2802_SET_REGISTER(0x03, "0x03");
  287. BD2802_SET_REGISTER(0x04, "0x04");
  288. BD2802_SET_REGISTER(0x05, "0x05");
  289. BD2802_SET_REGISTER(0x06, "0x06");
  290. BD2802_SET_REGISTER(0x07, "0x07");
  291. BD2802_SET_REGISTER(0x08, "0x08");
  292. BD2802_SET_REGISTER(0x09, "0x09");
  293. BD2802_SET_REGISTER(0x0a, "0x0a");
  294. BD2802_SET_REGISTER(0x0b, "0x0b");
  295. BD2802_SET_REGISTER(0x0c, "0x0c");
  296. BD2802_SET_REGISTER(0x0d, "0x0d");
  297. BD2802_SET_REGISTER(0x0e, "0x0e");
  298. BD2802_SET_REGISTER(0x0f, "0x0f");
  299. BD2802_SET_REGISTER(0x10, "0x10");
  300. BD2802_SET_REGISTER(0x11, "0x11");
  301. BD2802_SET_REGISTER(0x12, "0x12");
  302. BD2802_SET_REGISTER(0x13, "0x13");
  303. BD2802_SET_REGISTER(0x14, "0x14");
  304. BD2802_SET_REGISTER(0x15, "0x15");
  305. static struct device_attribute *bd2802_addr_attributes[] = {
  306. &bd2802_reg0x00_attr,
  307. &bd2802_reg0x01_attr,
  308. &bd2802_reg0x02_attr,
  309. &bd2802_reg0x03_attr,
  310. &bd2802_reg0x04_attr,
  311. &bd2802_reg0x05_attr,
  312. &bd2802_reg0x06_attr,
  313. &bd2802_reg0x07_attr,
  314. &bd2802_reg0x08_attr,
  315. &bd2802_reg0x09_attr,
  316. &bd2802_reg0x0a_attr,
  317. &bd2802_reg0x0b_attr,
  318. &bd2802_reg0x0c_attr,
  319. &bd2802_reg0x0d_attr,
  320. &bd2802_reg0x0e_attr,
  321. &bd2802_reg0x0f_attr,
  322. &bd2802_reg0x10_attr,
  323. &bd2802_reg0x11_attr,
  324. &bd2802_reg0x12_attr,
  325. &bd2802_reg0x13_attr,
  326. &bd2802_reg0x14_attr,
  327. &bd2802_reg0x15_attr,
  328. };
  329. static void bd2802_enable_adv_conf(struct bd2802_led *led)
  330. {
  331. int i, ret;
  332. for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++) {
  333. ret = device_create_file(&led->client->dev,
  334. bd2802_addr_attributes[i]);
  335. if (ret) {
  336. dev_err(&led->client->dev, "failed: sysfs file %s\n",
  337. bd2802_addr_attributes[i]->attr.name);
  338. goto failed_remove_files;
  339. }
  340. }
  341. if (bd2802_is_all_off(led))
  342. bd2802_reset_cancel(led);
  343. led->adf_on = 1;
  344. return;
  345. failed_remove_files:
  346. for (i--; i >= 0; i--)
  347. device_remove_file(&led->client->dev,
  348. bd2802_addr_attributes[i]);
  349. }
  350. static void bd2802_disable_adv_conf(struct bd2802_led *led)
  351. {
  352. int i;
  353. for (i = 0; i < ARRAY_SIZE(bd2802_addr_attributes); i++)
  354. device_remove_file(&led->client->dev,
  355. bd2802_addr_attributes[i]);
  356. if (bd2802_is_all_off(led))
  357. gpio_set_value(led->pdata->reset_gpio, 0);
  358. led->adf_on = 0;
  359. }
  360. static ssize_t bd2802_show_adv_conf(struct device *dev,
  361. struct device_attribute *attr, char *buf)
  362. {
  363. struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));
  364. ssize_t ret;
  365. down_read(&led->rwsem);
  366. if (led->adf_on)
  367. ret = sprintf(buf, "on\n");
  368. else
  369. ret = sprintf(buf, "off\n");
  370. up_read(&led->rwsem);
  371. return ret;
  372. }
  373. static ssize_t bd2802_store_adv_conf(struct device *dev,
  374. struct device_attribute *attr, const char *buf, size_t count)
  375. {
  376. struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));
  377. if (!count)
  378. return -EINVAL;
  379. down_write(&led->rwsem);
  380. if (!led->adf_on && !strncmp(buf, "on", 2))
  381. bd2802_enable_adv_conf(led);
  382. else if (led->adf_on && !strncmp(buf, "off", 3))
  383. bd2802_disable_adv_conf(led);
  384. up_write(&led->rwsem);
  385. return count;
  386. }
  387. static struct device_attribute bd2802_adv_conf_attr = {
  388. .attr = {
  389. .name = "advanced_configuration",
  390. .mode = 0644,
  391. },
  392. .show = bd2802_show_adv_conf,
  393. .store = bd2802_store_adv_conf,
  394. };
  395. #define BD2802_CONTROL_ATTR(attr_name, name_str) \
  396. static ssize_t bd2802_show_##attr_name(struct device *dev, \
  397. struct device_attribute *attr, char *buf) \
  398. { \
  399. struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
  400. ssize_t ret; \
  401. down_read(&led->rwsem); \
  402. ret = sprintf(buf, "0x%02x\n", led->attr_name); \
  403. up_read(&led->rwsem); \
  404. return ret; \
  405. } \
  406. static ssize_t bd2802_store_##attr_name(struct device *dev, \
  407. struct device_attribute *attr, const char *buf, size_t count) \
  408. { \
  409. struct bd2802_led *led = i2c_get_clientdata(to_i2c_client(dev));\
  410. unsigned long val; \
  411. int ret; \
  412. if (!count) \
  413. return -EINVAL; \
  414. ret = kstrtoul(buf, 16, &val); \
  415. if (ret) \
  416. return ret; \
  417. down_write(&led->rwsem); \
  418. led->attr_name = val; \
  419. up_write(&led->rwsem); \
  420. return count; \
  421. } \
  422. static struct device_attribute bd2802_##attr_name##_attr = { \
  423. .attr = { \
  424. .name = name_str, \
  425. .mode = 0644, \
  426. }, \
  427. .show = bd2802_show_##attr_name, \
  428. .store = bd2802_store_##attr_name, \
  429. };
  430. BD2802_CONTROL_ATTR(wave_pattern, "wave_pattern");
  431. BD2802_CONTROL_ATTR(rgb_current, "rgb_current");
  432. static struct device_attribute *bd2802_attributes[] = {
  433. &bd2802_adv_conf_attr,
  434. &bd2802_wave_pattern_attr,
  435. &bd2802_rgb_current_attr,
  436. };
  437. #define BD2802_CONTROL_RGBS(name, id, clr) \
  438. static int bd2802_set_##name##_brightness(struct led_classdev *led_cdev,\
  439. enum led_brightness value) \
  440. { \
  441. struct bd2802_led *led = \
  442. container_of(led_cdev, struct bd2802_led, cdev_##name); \
  443. led->led_id = id; \
  444. led->color = clr; \
  445. if (value == LED_OFF) { \
  446. led->state = BD2802_OFF; \
  447. bd2802_turn_off(led, led->led_id, led->color); \
  448. } else { \
  449. led->state = BD2802_ON; \
  450. bd2802_turn_on(led, led->led_id, led->color, BD2802_ON);\
  451. } \
  452. return 0; \
  453. } \
  454. static int bd2802_set_##name##_blink(struct led_classdev *led_cdev, \
  455. unsigned long *delay_on, unsigned long *delay_off) \
  456. { \
  457. struct bd2802_led *led = \
  458. container_of(led_cdev, struct bd2802_led, cdev_##name); \
  459. if (*delay_on == 0 || *delay_off == 0) \
  460. return -EINVAL; \
  461. led->led_id = id; \
  462. led->color = clr; \
  463. led->state = BD2802_BLINK; \
  464. bd2802_turn_on(led, led->led_id, led->color, BD2802_BLINK); \
  465. return 0; \
  466. }
  467. BD2802_CONTROL_RGBS(led1r, LED1, RED);
  468. BD2802_CONTROL_RGBS(led1g, LED1, GREEN);
  469. BD2802_CONTROL_RGBS(led1b, LED1, BLUE);
  470. BD2802_CONTROL_RGBS(led2r, LED2, RED);
  471. BD2802_CONTROL_RGBS(led2g, LED2, GREEN);
  472. BD2802_CONTROL_RGBS(led2b, LED2, BLUE);
  473. static int bd2802_register_led_classdev(struct bd2802_led *led)
  474. {
  475. int ret;
  476. led->cdev_led1r.name = "led1_R";
  477. led->cdev_led1r.brightness = LED_OFF;
  478. led->cdev_led1r.brightness_set_blocking = bd2802_set_led1r_brightness;
  479. led->cdev_led1r.blink_set = bd2802_set_led1r_blink;
  480. ret = led_classdev_register(&led->client->dev, &led->cdev_led1r);
  481. if (ret < 0) {
  482. dev_err(&led->client->dev, "couldn't register LED %s\n",
  483. led->cdev_led1r.name);
  484. goto failed_unregister_led1_R;
  485. }
  486. led->cdev_led1g.name = "led1_G";
  487. led->cdev_led1g.brightness = LED_OFF;
  488. led->cdev_led1g.brightness_set_blocking = bd2802_set_led1g_brightness;
  489. led->cdev_led1g.blink_set = bd2802_set_led1g_blink;
  490. ret = led_classdev_register(&led->client->dev, &led->cdev_led1g);
  491. if (ret < 0) {
  492. dev_err(&led->client->dev, "couldn't register LED %s\n",
  493. led->cdev_led1g.name);
  494. goto failed_unregister_led1_G;
  495. }
  496. led->cdev_led1b.name = "led1_B";
  497. led->cdev_led1b.brightness = LED_OFF;
  498. led->cdev_led1b.brightness_set_blocking = bd2802_set_led1b_brightness;
  499. led->cdev_led1b.blink_set = bd2802_set_led1b_blink;
  500. ret = led_classdev_register(&led->client->dev, &led->cdev_led1b);
  501. if (ret < 0) {
  502. dev_err(&led->client->dev, "couldn't register LED %s\n",
  503. led->cdev_led1b.name);
  504. goto failed_unregister_led1_B;
  505. }
  506. led->cdev_led2r.name = "led2_R";
  507. led->cdev_led2r.brightness = LED_OFF;
  508. led->cdev_led2r.brightness_set_blocking = bd2802_set_led2r_brightness;
  509. led->cdev_led2r.blink_set = bd2802_set_led2r_blink;
  510. ret = led_classdev_register(&led->client->dev, &led->cdev_led2r);
  511. if (ret < 0) {
  512. dev_err(&led->client->dev, "couldn't register LED %s\n",
  513. led->cdev_led2r.name);
  514. goto failed_unregister_led2_R;
  515. }
  516. led->cdev_led2g.name = "led2_G";
  517. led->cdev_led2g.brightness = LED_OFF;
  518. led->cdev_led2g.brightness_set_blocking = bd2802_set_led2g_brightness;
  519. led->cdev_led2g.blink_set = bd2802_set_led2g_blink;
  520. ret = led_classdev_register(&led->client->dev, &led->cdev_led2g);
  521. if (ret < 0) {
  522. dev_err(&led->client->dev, "couldn't register LED %s\n",
  523. led->cdev_led2g.name);
  524. goto failed_unregister_led2_G;
  525. }
  526. led->cdev_led2b.name = "led2_B";
  527. led->cdev_led2b.brightness = LED_OFF;
  528. led->cdev_led2b.brightness_set_blocking = bd2802_set_led2b_brightness;
  529. led->cdev_led2b.blink_set = bd2802_set_led2b_blink;
  530. led->cdev_led2b.flags |= LED_CORE_SUSPENDRESUME;
  531. ret = led_classdev_register(&led->client->dev, &led->cdev_led2b);
  532. if (ret < 0) {
  533. dev_err(&led->client->dev, "couldn't register LED %s\n",
  534. led->cdev_led2b.name);
  535. goto failed_unregister_led2_B;
  536. }
  537. return 0;
  538. failed_unregister_led2_B:
  539. led_classdev_unregister(&led->cdev_led2g);
  540. failed_unregister_led2_G:
  541. led_classdev_unregister(&led->cdev_led2r);
  542. failed_unregister_led2_R:
  543. led_classdev_unregister(&led->cdev_led1b);
  544. failed_unregister_led1_B:
  545. led_classdev_unregister(&led->cdev_led1g);
  546. failed_unregister_led1_G:
  547. led_classdev_unregister(&led->cdev_led1r);
  548. failed_unregister_led1_R:
  549. return ret;
  550. }
  551. static void bd2802_unregister_led_classdev(struct bd2802_led *led)
  552. {
  553. led_classdev_unregister(&led->cdev_led2b);
  554. led_classdev_unregister(&led->cdev_led2g);
  555. led_classdev_unregister(&led->cdev_led2r);
  556. led_classdev_unregister(&led->cdev_led1b);
  557. led_classdev_unregister(&led->cdev_led1g);
  558. led_classdev_unregister(&led->cdev_led1r);
  559. }
  560. static int bd2802_probe(struct i2c_client *client,
  561. const struct i2c_device_id *id)
  562. {
  563. struct bd2802_led *led;
  564. struct bd2802_led_platform_data *pdata;
  565. int ret, i;
  566. led = devm_kzalloc(&client->dev, sizeof(struct bd2802_led), GFP_KERNEL);
  567. if (!led)
  568. return -ENOMEM;
  569. led->client = client;
  570. pdata = led->pdata = dev_get_platdata(&client->dev);
  571. i2c_set_clientdata(client, led);
  572. /* Configure RESET GPIO (L: RESET, H: RESET cancel) */
  573. gpio_request_one(pdata->reset_gpio, GPIOF_OUT_INIT_HIGH, "RGB_RESETB");
  574. /* Tacss = min 0.1ms */
  575. udelay(100);
  576. /* Detect BD2802GU */
  577. ret = bd2802_write_byte(client, BD2802_REG_CLKSETUP, 0x00);
  578. if (ret < 0) {
  579. dev_err(&client->dev, "failed to detect device\n");
  580. return ret;
  581. } else
  582. dev_info(&client->dev, "return 0x%02x\n", ret);
  583. /* To save the power, reset BD2802 after detecting */
  584. gpio_set_value(led->pdata->reset_gpio, 0);
  585. /* Default attributes */
  586. led->wave_pattern = BD2802_PATTERN_HALF;
  587. led->rgb_current = BD2802_CURRENT_032;
  588. init_rwsem(&led->rwsem);
  589. for (i = 0; i < ARRAY_SIZE(bd2802_attributes); i++) {
  590. ret = device_create_file(&led->client->dev,
  591. bd2802_attributes[i]);
  592. if (ret) {
  593. dev_err(&led->client->dev, "failed: sysfs file %s\n",
  594. bd2802_attributes[i]->attr.name);
  595. goto failed_unregister_dev_file;
  596. }
  597. }
  598. ret = bd2802_register_led_classdev(led);
  599. if (ret < 0)
  600. goto failed_unregister_dev_file;
  601. return 0;
  602. failed_unregister_dev_file:
  603. for (i--; i >= 0; i--)
  604. device_remove_file(&led->client->dev, bd2802_attributes[i]);
  605. return ret;
  606. }
  607. static int bd2802_remove(struct i2c_client *client)
  608. {
  609. struct bd2802_led *led = i2c_get_clientdata(client);
  610. int i;
  611. gpio_set_value(led->pdata->reset_gpio, 0);
  612. bd2802_unregister_led_classdev(led);
  613. if (led->adf_on)
  614. bd2802_disable_adv_conf(led);
  615. for (i = 0; i < ARRAY_SIZE(bd2802_attributes); i++)
  616. device_remove_file(&led->client->dev, bd2802_attributes[i]);
  617. return 0;
  618. }
  619. #ifdef CONFIG_PM_SLEEP
  620. static void bd2802_restore_state(struct bd2802_led *led)
  621. {
  622. int i;
  623. for (i = 0; i < LED_NUM; i++) {
  624. if (led->led[i].r)
  625. bd2802_turn_on(led, i, RED, led->led[i].r);
  626. if (led->led[i].g)
  627. bd2802_turn_on(led, i, GREEN, led->led[i].g);
  628. if (led->led[i].b)
  629. bd2802_turn_on(led, i, BLUE, led->led[i].b);
  630. }
  631. }
  632. static int bd2802_suspend(struct device *dev)
  633. {
  634. struct i2c_client *client = to_i2c_client(dev);
  635. struct bd2802_led *led = i2c_get_clientdata(client);
  636. gpio_set_value(led->pdata->reset_gpio, 0);
  637. return 0;
  638. }
  639. static int bd2802_resume(struct device *dev)
  640. {
  641. struct i2c_client *client = to_i2c_client(dev);
  642. struct bd2802_led *led = i2c_get_clientdata(client);
  643. if (!bd2802_is_all_off(led) || led->adf_on) {
  644. bd2802_reset_cancel(led);
  645. bd2802_restore_state(led);
  646. }
  647. return 0;
  648. }
  649. #endif
  650. static SIMPLE_DEV_PM_OPS(bd2802_pm, bd2802_suspend, bd2802_resume);
  651. static const struct i2c_device_id bd2802_id[] = {
  652. { "BD2802", 0 },
  653. { }
  654. };
  655. MODULE_DEVICE_TABLE(i2c, bd2802_id);
  656. static struct i2c_driver bd2802_i2c_driver = {
  657. .driver = {
  658. .name = "BD2802",
  659. .pm = &bd2802_pm,
  660. },
  661. .probe = bd2802_probe,
  662. .remove = bd2802_remove,
  663. .id_table = bd2802_id,
  664. };
  665. module_i2c_driver(bd2802_i2c_driver);
  666. MODULE_AUTHOR("Kim Kyuwon <q1.kim@samsung.com>");
  667. MODULE_DESCRIPTION("BD2802 LED driver");
  668. MODULE_LICENSE("GPL v2");