leds-bd2802.c 21 KB

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