leds-lp5521.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*
  2. * LP5521 LED chip driver.
  3. *
  4. * Copyright (C) 2010 Nokia Corporation
  5. *
  6. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/i2c.h>
  25. #include <linux/mutex.h>
  26. #include <linux/gpio.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/ctype.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/wait.h>
  32. #include <linux/leds.h>
  33. #include <linux/leds-lp5521.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/slab.h>
  36. #define LP5521_PROGRAM_LENGTH 32 /* in bytes */
  37. #define LP5521_MAX_LEDS 3 /* Maximum number of LEDs */
  38. #define LP5521_MAX_ENGINES 3 /* Maximum number of engines */
  39. #define LP5521_ENG_MASK_BASE 0x30 /* 00110000 */
  40. #define LP5521_ENG_STATUS_MASK 0x07 /* 00000111 */
  41. #define LP5521_CMD_LOAD 0x15 /* 00010101 */
  42. #define LP5521_CMD_RUN 0x2a /* 00101010 */
  43. #define LP5521_CMD_DIRECT 0x3f /* 00111111 */
  44. #define LP5521_CMD_DISABLED 0x00 /* 00000000 */
  45. /* Registers */
  46. #define LP5521_REG_ENABLE 0x00
  47. #define LP5521_REG_OP_MODE 0x01
  48. #define LP5521_REG_R_PWM 0x02
  49. #define LP5521_REG_G_PWM 0x03
  50. #define LP5521_REG_B_PWM 0x04
  51. #define LP5521_REG_R_CURRENT 0x05
  52. #define LP5521_REG_G_CURRENT 0x06
  53. #define LP5521_REG_B_CURRENT 0x07
  54. #define LP5521_REG_CONFIG 0x08
  55. #define LP5521_REG_R_CHANNEL_PC 0x09
  56. #define LP5521_REG_G_CHANNEL_PC 0x0A
  57. #define LP5521_REG_B_CHANNEL_PC 0x0B
  58. #define LP5521_REG_STATUS 0x0C
  59. #define LP5521_REG_RESET 0x0D
  60. #define LP5521_REG_GPO 0x0E
  61. #define LP5521_REG_R_PROG_MEM 0x10
  62. #define LP5521_REG_G_PROG_MEM 0x30
  63. #define LP5521_REG_B_PROG_MEM 0x50
  64. #define LP5521_PROG_MEM_BASE LP5521_REG_R_PROG_MEM
  65. #define LP5521_PROG_MEM_SIZE 0x20
  66. /* Base register to set LED current */
  67. #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT
  68. /* Base register to set the brightness */
  69. #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM
  70. /* Bits in ENABLE register */
  71. #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */
  72. #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */
  73. #define LP5521_EXEC_RUN 0x2A
  74. /* Bits in CONFIG register */
  75. #define LP5521_PWM_HF 0x40 /* PWM: 0 = 256Hz, 1 = 558Hz */
  76. #define LP5521_PWRSAVE_EN 0x20 /* 1 = Power save mode */
  77. #define LP5521_CP_MODE_OFF 0 /* Charge pump (CP) off */
  78. #define LP5521_CP_MODE_BYPASS 8 /* CP forced to bypass mode */
  79. #define LP5521_CP_MODE_1X5 0x10 /* CP forced to 1.5x mode */
  80. #define LP5521_CP_MODE_AUTO 0x18 /* Automatic mode selection */
  81. #define LP5521_R_TO_BATT 4 /* R out: 0 = CP, 1 = Vbat */
  82. #define LP5521_CLK_SRC_EXT 0 /* Ext-clk source (CLK_32K) */
  83. #define LP5521_CLK_INT 1 /* Internal clock */
  84. #define LP5521_CLK_AUTO 2 /* Automatic clock selection */
  85. /* Status */
  86. #define LP5521_EXT_CLK_USED 0x08
  87. /* default R channel current register value */
  88. #define LP5521_REG_R_CURR_DEFAULT 0xAF
  89. struct lp5521_engine {
  90. int id;
  91. u8 mode;
  92. u8 prog_page;
  93. u8 engine_mask;
  94. };
  95. struct lp5521_led {
  96. int id;
  97. u8 chan_nr;
  98. u8 led_current;
  99. u8 max_current;
  100. struct led_classdev cdev;
  101. struct work_struct brightness_work;
  102. u8 brightness;
  103. };
  104. struct lp5521_chip {
  105. struct lp5521_platform_data *pdata;
  106. struct mutex lock; /* Serialize control */
  107. struct i2c_client *client;
  108. struct lp5521_engine engines[LP5521_MAX_ENGINES];
  109. struct lp5521_led leds[LP5521_MAX_LEDS];
  110. u8 num_channels;
  111. u8 num_leds;
  112. };
  113. static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
  114. {
  115. return container_of(cdev, struct lp5521_led, cdev);
  116. }
  117. static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
  118. {
  119. return container_of(engine, struct lp5521_chip,
  120. engines[engine->id - 1]);
  121. }
  122. static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
  123. {
  124. return container_of(led, struct lp5521_chip,
  125. leds[led->id]);
  126. }
  127. static void lp5521_led_brightness_work(struct work_struct *work);
  128. static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
  129. {
  130. return i2c_smbus_write_byte_data(client, reg, value);
  131. }
  132. static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
  133. {
  134. s32 ret;
  135. ret = i2c_smbus_read_byte_data(client, reg);
  136. if (ret < 0)
  137. return -EIO;
  138. *buf = ret;
  139. return 0;
  140. }
  141. static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
  142. {
  143. struct lp5521_chip *chip = engine_to_lp5521(engine);
  144. struct i2c_client *client = chip->client;
  145. int ret;
  146. u8 engine_state;
  147. /* Only transition between RUN and DIRECT mode are handled here */
  148. if (mode == LP5521_CMD_LOAD)
  149. return 0;
  150. if (mode == LP5521_CMD_DISABLED)
  151. mode = LP5521_CMD_DIRECT;
  152. ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
  153. if (ret < 0)
  154. return ret;
  155. /* set mode only for this engine */
  156. engine_state &= ~(engine->engine_mask);
  157. mode &= engine->engine_mask;
  158. engine_state |= mode;
  159. return lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
  160. }
  161. static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
  162. {
  163. struct lp5521_chip *chip = engine_to_lp5521(eng);
  164. struct i2c_client *client = chip->client;
  165. int ret;
  166. int addr;
  167. u8 mode;
  168. /* move current engine to direct mode and remember the state */
  169. ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
  170. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  171. usleep_range(1000, 2000);
  172. ret |= lp5521_read(client, LP5521_REG_OP_MODE, &mode);
  173. /* For loading, all the engines to load mode */
  174. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  175. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  176. usleep_range(1000, 2000);
  177. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
  178. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  179. usleep_range(1000, 2000);
  180. addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
  181. i2c_smbus_write_i2c_block_data(client,
  182. addr,
  183. LP5521_PROG_MEM_SIZE,
  184. pattern);
  185. ret |= lp5521_write(client, LP5521_REG_OP_MODE, mode);
  186. return ret;
  187. }
  188. static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
  189. {
  190. return lp5521_write(chip->client,
  191. LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
  192. curr);
  193. }
  194. static void lp5521_init_engine(struct lp5521_chip *chip)
  195. {
  196. int i;
  197. for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
  198. chip->engines[i].id = i + 1;
  199. chip->engines[i].engine_mask = LP5521_ENG_MASK_BASE >> (i * 2);
  200. chip->engines[i].prog_page = i;
  201. }
  202. }
  203. static int lp5521_configure(struct i2c_client *client)
  204. {
  205. struct lp5521_chip *chip = i2c_get_clientdata(client);
  206. int ret;
  207. lp5521_init_engine(chip);
  208. /* Set all PWMs to direct control mode */
  209. ret = lp5521_write(client, LP5521_REG_OP_MODE, 0x3F);
  210. /* Enable auto-powersave, set charge pump to auto, red to battery */
  211. ret |= lp5521_write(client, LP5521_REG_CONFIG,
  212. LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
  213. /* Initialize all channels PWM to zero -> leds off */
  214. ret |= lp5521_write(client, LP5521_REG_R_PWM, 0);
  215. ret |= lp5521_write(client, LP5521_REG_G_PWM, 0);
  216. ret |= lp5521_write(client, LP5521_REG_B_PWM, 0);
  217. /* Set engines are set to run state when OP_MODE enables engines */
  218. ret |= lp5521_write(client, LP5521_REG_ENABLE,
  219. LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM |
  220. LP5521_EXEC_RUN);
  221. /* enable takes 500us. 1 - 2 ms leaves some margin */
  222. usleep_range(1000, 2000);
  223. return ret;
  224. }
  225. static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
  226. {
  227. int ret;
  228. u8 status;
  229. ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
  230. if (ret < 0)
  231. return ret;
  232. /* Check that ext clock is really in use if requested */
  233. if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
  234. if ((status & LP5521_EXT_CLK_USED) == 0)
  235. return -EIO;
  236. return 0;
  237. }
  238. static void lp5521_set_brightness(struct led_classdev *cdev,
  239. enum led_brightness brightness)
  240. {
  241. struct lp5521_led *led = cdev_to_led(cdev);
  242. led->brightness = (u8)brightness;
  243. schedule_work(&led->brightness_work);
  244. }
  245. static void lp5521_led_brightness_work(struct work_struct *work)
  246. {
  247. struct lp5521_led *led = container_of(work,
  248. struct lp5521_led,
  249. brightness_work);
  250. struct lp5521_chip *chip = led_to_lp5521(led);
  251. struct i2c_client *client = chip->client;
  252. mutex_lock(&chip->lock);
  253. lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
  254. led->brightness);
  255. mutex_unlock(&chip->lock);
  256. }
  257. /* Detect the chip by setting its ENABLE register and reading it back. */
  258. static int lp5521_detect(struct i2c_client *client)
  259. {
  260. int ret;
  261. u8 buf;
  262. ret = lp5521_write(client, LP5521_REG_ENABLE,
  263. LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM);
  264. if (ret)
  265. return ret;
  266. /* enable takes 500us. 1 - 2 ms leaves some margin */
  267. usleep_range(1000, 2000);
  268. ret = lp5521_read(client, LP5521_REG_ENABLE, &buf);
  269. if (ret)
  270. return ret;
  271. if (buf != (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM))
  272. return -ENODEV;
  273. return 0;
  274. }
  275. /* Set engine mode and create appropriate sysfs attributes, if required. */
  276. static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
  277. {
  278. int ret = 0;
  279. /* if in that mode already do nothing, except for run */
  280. if (mode == engine->mode && mode != LP5521_CMD_RUN)
  281. return 0;
  282. if (mode == LP5521_CMD_RUN) {
  283. ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
  284. } else if (mode == LP5521_CMD_LOAD) {
  285. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  286. lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
  287. } else if (mode == LP5521_CMD_DISABLED) {
  288. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  289. }
  290. engine->mode = mode;
  291. return ret;
  292. }
  293. static int lp5521_do_store_load(struct lp5521_engine *engine,
  294. const char *buf, size_t len)
  295. {
  296. struct lp5521_chip *chip = engine_to_lp5521(engine);
  297. struct i2c_client *client = chip->client;
  298. int ret, nrchars, offset = 0, i = 0;
  299. char c[3];
  300. unsigned cmd;
  301. u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
  302. while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
  303. /* separate sscanfs because length is working only for %s */
  304. ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
  305. if (ret != 2)
  306. goto fail;
  307. ret = sscanf(c, "%2x", &cmd);
  308. if (ret != 1)
  309. goto fail;
  310. pattern[i] = (u8)cmd;
  311. offset += nrchars;
  312. i++;
  313. }
  314. /* Each instruction is 16bit long. Check that length is even */
  315. if (i % 2)
  316. goto fail;
  317. mutex_lock(&chip->lock);
  318. if (engine->mode == LP5521_CMD_LOAD)
  319. ret = lp5521_load_program(engine, pattern);
  320. else
  321. ret = -EINVAL;
  322. mutex_unlock(&chip->lock);
  323. if (ret) {
  324. dev_err(&client->dev, "failed loading pattern\n");
  325. return ret;
  326. }
  327. return len;
  328. fail:
  329. dev_err(&client->dev, "wrong pattern format\n");
  330. return -EINVAL;
  331. }
  332. static ssize_t store_engine_load(struct device *dev,
  333. struct device_attribute *attr,
  334. const char *buf, size_t len, int nr)
  335. {
  336. struct i2c_client *client = to_i2c_client(dev);
  337. struct lp5521_chip *chip = i2c_get_clientdata(client);
  338. return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
  339. }
  340. #define store_load(nr) \
  341. static ssize_t store_engine##nr##_load(struct device *dev, \
  342. struct device_attribute *attr, \
  343. const char *buf, size_t len) \
  344. { \
  345. return store_engine_load(dev, attr, buf, len, nr); \
  346. }
  347. store_load(1)
  348. store_load(2)
  349. store_load(3)
  350. static ssize_t show_engine_mode(struct device *dev,
  351. struct device_attribute *attr,
  352. char *buf, int nr)
  353. {
  354. struct i2c_client *client = to_i2c_client(dev);
  355. struct lp5521_chip *chip = i2c_get_clientdata(client);
  356. switch (chip->engines[nr - 1].mode) {
  357. case LP5521_CMD_RUN:
  358. return sprintf(buf, "run\n");
  359. case LP5521_CMD_LOAD:
  360. return sprintf(buf, "load\n");
  361. case LP5521_CMD_DISABLED:
  362. return sprintf(buf, "disabled\n");
  363. default:
  364. return sprintf(buf, "disabled\n");
  365. }
  366. }
  367. #define show_mode(nr) \
  368. static ssize_t show_engine##nr##_mode(struct device *dev, \
  369. struct device_attribute *attr, \
  370. char *buf) \
  371. { \
  372. return show_engine_mode(dev, attr, buf, nr); \
  373. }
  374. show_mode(1)
  375. show_mode(2)
  376. show_mode(3)
  377. static ssize_t store_engine_mode(struct device *dev,
  378. struct device_attribute *attr,
  379. const char *buf, size_t len, int nr)
  380. {
  381. struct i2c_client *client = to_i2c_client(dev);
  382. struct lp5521_chip *chip = i2c_get_clientdata(client);
  383. struct lp5521_engine *engine = &chip->engines[nr - 1];
  384. mutex_lock(&chip->lock);
  385. if (!strncmp(buf, "run", 3))
  386. lp5521_set_mode(engine, LP5521_CMD_RUN);
  387. else if (!strncmp(buf, "load", 4))
  388. lp5521_set_mode(engine, LP5521_CMD_LOAD);
  389. else if (!strncmp(buf, "disabled", 8))
  390. lp5521_set_mode(engine, LP5521_CMD_DISABLED);
  391. mutex_unlock(&chip->lock);
  392. return len;
  393. }
  394. #define store_mode(nr) \
  395. static ssize_t store_engine##nr##_mode(struct device *dev, \
  396. struct device_attribute *attr, \
  397. const char *buf, size_t len) \
  398. { \
  399. return store_engine_mode(dev, attr, buf, len, nr); \
  400. }
  401. store_mode(1)
  402. store_mode(2)
  403. store_mode(3)
  404. static ssize_t show_max_current(struct device *dev,
  405. struct device_attribute *attr,
  406. char *buf)
  407. {
  408. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  409. struct lp5521_led *led = cdev_to_led(led_cdev);
  410. return sprintf(buf, "%d\n", led->max_current);
  411. }
  412. static ssize_t show_current(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  417. struct lp5521_led *led = cdev_to_led(led_cdev);
  418. return sprintf(buf, "%d\n", led->led_current);
  419. }
  420. static ssize_t store_current(struct device *dev,
  421. struct device_attribute *attr,
  422. const char *buf, size_t len)
  423. {
  424. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  425. struct lp5521_led *led = cdev_to_led(led_cdev);
  426. struct lp5521_chip *chip = led_to_lp5521(led);
  427. ssize_t ret;
  428. unsigned long curr;
  429. if (strict_strtoul(buf, 0, &curr))
  430. return -EINVAL;
  431. if (curr > led->max_current)
  432. return -EINVAL;
  433. mutex_lock(&chip->lock);
  434. ret = lp5521_set_led_current(chip, led->id, curr);
  435. mutex_unlock(&chip->lock);
  436. if (ret < 0)
  437. return ret;
  438. led->led_current = (u8)curr;
  439. return len;
  440. }
  441. static ssize_t lp5521_selftest(struct device *dev,
  442. struct device_attribute *attr,
  443. char *buf)
  444. {
  445. struct i2c_client *client = to_i2c_client(dev);
  446. struct lp5521_chip *chip = i2c_get_clientdata(client);
  447. int ret;
  448. mutex_lock(&chip->lock);
  449. ret = lp5521_run_selftest(chip, buf);
  450. mutex_unlock(&chip->lock);
  451. return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
  452. }
  453. /* led class device attributes */
  454. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, show_current, store_current);
  455. static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
  456. static struct attribute *lp5521_led_attributes[] = {
  457. &dev_attr_led_current.attr,
  458. &dev_attr_max_current.attr,
  459. NULL,
  460. };
  461. static struct attribute_group lp5521_led_attribute_group = {
  462. .attrs = lp5521_led_attributes
  463. };
  464. /* device attributes */
  465. static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUSR,
  466. show_engine1_mode, store_engine1_mode);
  467. static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUSR,
  468. show_engine2_mode, store_engine2_mode);
  469. static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUSR,
  470. show_engine3_mode, store_engine3_mode);
  471. static DEVICE_ATTR(engine1_load, S_IWUSR, NULL, store_engine1_load);
  472. static DEVICE_ATTR(engine2_load, S_IWUSR, NULL, store_engine2_load);
  473. static DEVICE_ATTR(engine3_load, S_IWUSR, NULL, store_engine3_load);
  474. static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
  475. static struct attribute *lp5521_attributes[] = {
  476. &dev_attr_engine1_mode.attr,
  477. &dev_attr_engine2_mode.attr,
  478. &dev_attr_engine3_mode.attr,
  479. &dev_attr_selftest.attr,
  480. &dev_attr_engine1_load.attr,
  481. &dev_attr_engine2_load.attr,
  482. &dev_attr_engine3_load.attr,
  483. NULL
  484. };
  485. static const struct attribute_group lp5521_group = {
  486. .attrs = lp5521_attributes,
  487. };
  488. static int lp5521_register_sysfs(struct i2c_client *client)
  489. {
  490. struct device *dev = &client->dev;
  491. return sysfs_create_group(&dev->kobj, &lp5521_group);
  492. }
  493. static void lp5521_unregister_sysfs(struct i2c_client *client)
  494. {
  495. struct lp5521_chip *chip = i2c_get_clientdata(client);
  496. struct device *dev = &client->dev;
  497. int i;
  498. sysfs_remove_group(&dev->kobj, &lp5521_group);
  499. for (i = 0; i < chip->num_leds; i++)
  500. sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
  501. &lp5521_led_attribute_group);
  502. }
  503. static int __devinit lp5521_init_led(struct lp5521_led *led,
  504. struct i2c_client *client,
  505. int chan, struct lp5521_platform_data *pdata)
  506. {
  507. struct device *dev = &client->dev;
  508. char name[32];
  509. int res;
  510. if (chan >= LP5521_MAX_LEDS)
  511. return -EINVAL;
  512. if (pdata->led_config[chan].led_current == 0)
  513. return 0;
  514. led->led_current = pdata->led_config[chan].led_current;
  515. led->max_current = pdata->led_config[chan].max_current;
  516. led->chan_nr = pdata->led_config[chan].chan_nr;
  517. if (led->chan_nr >= LP5521_MAX_LEDS) {
  518. dev_err(dev, "Use channel numbers between 0 and %d\n",
  519. LP5521_MAX_LEDS - 1);
  520. return -EINVAL;
  521. }
  522. snprintf(name, sizeof(name), "%s:channel%d",
  523. pdata->label ?: client->name, chan);
  524. led->cdev.brightness_set = lp5521_set_brightness;
  525. led->cdev.name = name;
  526. res = led_classdev_register(dev, &led->cdev);
  527. if (res < 0) {
  528. dev_err(dev, "couldn't register led on channel %d\n", chan);
  529. return res;
  530. }
  531. res = sysfs_create_group(&led->cdev.dev->kobj,
  532. &lp5521_led_attribute_group);
  533. if (res < 0) {
  534. dev_err(dev, "couldn't register current attribute\n");
  535. led_classdev_unregister(&led->cdev);
  536. return res;
  537. }
  538. return 0;
  539. }
  540. static int __devinit lp5521_probe(struct i2c_client *client,
  541. const struct i2c_device_id *id)
  542. {
  543. struct lp5521_chip *chip;
  544. struct lp5521_platform_data *pdata;
  545. int ret, i, led;
  546. u8 buf;
  547. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  548. if (!chip)
  549. return -ENOMEM;
  550. i2c_set_clientdata(client, chip);
  551. chip->client = client;
  552. pdata = client->dev.platform_data;
  553. if (!pdata) {
  554. dev_err(&client->dev, "no platform data\n");
  555. ret = -EINVAL;
  556. goto fail1;
  557. }
  558. mutex_init(&chip->lock);
  559. chip->pdata = pdata;
  560. if (pdata->setup_resources) {
  561. ret = pdata->setup_resources();
  562. if (ret < 0)
  563. goto fail1;
  564. }
  565. if (pdata->enable) {
  566. pdata->enable(0);
  567. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  568. pdata->enable(1);
  569. usleep_range(1000, 2000); /* 500us abs min. */
  570. }
  571. lp5521_write(client, LP5521_REG_RESET, 0xff);
  572. usleep_range(10000, 20000); /*
  573. * Exact value is not available. 10 - 20ms
  574. * appears to be enough for reset.
  575. */
  576. /*
  577. * Make sure that the chip is reset by reading back the r channel
  578. * current reg. This is dummy read is required on some platforms -
  579. * otherwise further access to the R G B channels in the
  580. * LP5521_REG_ENABLE register will not have any effect - strange!
  581. */
  582. lp5521_read(client, LP5521_REG_R_CURRENT, &buf);
  583. if (buf != LP5521_REG_R_CURR_DEFAULT) {
  584. dev_err(&client->dev, "error in reseting chip\n");
  585. goto fail2;
  586. }
  587. usleep_range(10000, 20000);
  588. ret = lp5521_detect(client);
  589. if (ret) {
  590. dev_err(&client->dev, "Chip not found\n");
  591. goto fail2;
  592. }
  593. dev_info(&client->dev, "%s programmable led chip found\n", id->name);
  594. ret = lp5521_configure(client);
  595. if (ret < 0) {
  596. dev_err(&client->dev, "error configuring chip\n");
  597. goto fail2;
  598. }
  599. /* Initialize leds */
  600. chip->num_channels = pdata->num_channels;
  601. chip->num_leds = 0;
  602. led = 0;
  603. for (i = 0; i < pdata->num_channels; i++) {
  604. /* Do not initialize channels that are not connected */
  605. if (pdata->led_config[i].led_current == 0)
  606. continue;
  607. ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
  608. if (ret) {
  609. dev_err(&client->dev, "error initializing leds\n");
  610. goto fail3;
  611. }
  612. chip->num_leds++;
  613. chip->leds[led].id = led;
  614. /* Set initial LED current */
  615. lp5521_set_led_current(chip, led,
  616. chip->leds[led].led_current);
  617. INIT_WORK(&(chip->leds[led].brightness_work),
  618. lp5521_led_brightness_work);
  619. led++;
  620. }
  621. ret = lp5521_register_sysfs(client);
  622. if (ret) {
  623. dev_err(&client->dev, "registering sysfs failed\n");
  624. goto fail3;
  625. }
  626. return ret;
  627. fail3:
  628. for (i = 0; i < chip->num_leds; i++) {
  629. led_classdev_unregister(&chip->leds[i].cdev);
  630. cancel_work_sync(&chip->leds[i].brightness_work);
  631. }
  632. fail2:
  633. if (pdata->enable)
  634. pdata->enable(0);
  635. if (pdata->release_resources)
  636. pdata->release_resources();
  637. fail1:
  638. kfree(chip);
  639. return ret;
  640. }
  641. static int __devexit lp5521_remove(struct i2c_client *client)
  642. {
  643. struct lp5521_chip *chip = i2c_get_clientdata(client);
  644. int i;
  645. lp5521_unregister_sysfs(client);
  646. for (i = 0; i < chip->num_leds; i++) {
  647. led_classdev_unregister(&chip->leds[i].cdev);
  648. cancel_work_sync(&chip->leds[i].brightness_work);
  649. }
  650. if (chip->pdata->enable)
  651. chip->pdata->enable(0);
  652. if (chip->pdata->release_resources)
  653. chip->pdata->release_resources();
  654. kfree(chip);
  655. return 0;
  656. }
  657. static const struct i2c_device_id lp5521_id[] = {
  658. { "lp5521", 0 }, /* Three channel chip */
  659. { }
  660. };
  661. MODULE_DEVICE_TABLE(i2c, lp5521_id);
  662. static struct i2c_driver lp5521_driver = {
  663. .driver = {
  664. .name = "lp5521",
  665. },
  666. .probe = lp5521_probe,
  667. .remove = __devexit_p(lp5521_remove),
  668. .id_table = lp5521_id,
  669. };
  670. static int __init lp5521_init(void)
  671. {
  672. int ret;
  673. ret = i2c_add_driver(&lp5521_driver);
  674. if (ret < 0)
  675. printk(KERN_ALERT "Adding lp5521 driver failed\n");
  676. return ret;
  677. }
  678. static void __exit lp5521_exit(void)
  679. {
  680. i2c_del_driver(&lp5521_driver);
  681. }
  682. module_init(lp5521_init);
  683. module_exit(lp5521_exit);
  684. MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
  685. MODULE_DESCRIPTION("LP5521 LED engine");
  686. MODULE_LICENSE("GPL v2");