leds-lp5521.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. struct lp5521_engine {
  88. const struct attribute_group *attributes;
  89. int id;
  90. u8 mode;
  91. u8 prog_page;
  92. u8 engine_mask;
  93. };
  94. struct lp5521_led {
  95. int id;
  96. u8 chan_nr;
  97. u8 led_current;
  98. u8 max_current;
  99. struct led_classdev cdev;
  100. struct work_struct brightness_work;
  101. u8 brightness;
  102. };
  103. struct lp5521_chip {
  104. struct lp5521_platform_data *pdata;
  105. struct mutex lock; /* Serialize control */
  106. struct i2c_client *client;
  107. struct lp5521_engine engines[LP5521_MAX_ENGINES];
  108. struct lp5521_led leds[LP5521_MAX_LEDS];
  109. u8 num_channels;
  110. u8 num_leds;
  111. };
  112. static inline struct lp5521_led *cdev_to_led(struct led_classdev *cdev)
  113. {
  114. return container_of(cdev, struct lp5521_led, cdev);
  115. }
  116. static inline struct lp5521_chip *engine_to_lp5521(struct lp5521_engine *engine)
  117. {
  118. return container_of(engine, struct lp5521_chip,
  119. engines[engine->id - 1]);
  120. }
  121. static inline struct lp5521_chip *led_to_lp5521(struct lp5521_led *led)
  122. {
  123. return container_of(led, struct lp5521_chip,
  124. leds[led->id]);
  125. }
  126. static void lp5521_led_brightness_work(struct work_struct *work);
  127. static inline int lp5521_write(struct i2c_client *client, u8 reg, u8 value)
  128. {
  129. return i2c_smbus_write_byte_data(client, reg, value);
  130. }
  131. static int lp5521_read(struct i2c_client *client, u8 reg, u8 *buf)
  132. {
  133. s32 ret;
  134. ret = i2c_smbus_read_byte_data(client, reg);
  135. if (ret < 0)
  136. return -EIO;
  137. *buf = ret;
  138. return 0;
  139. }
  140. static int lp5521_set_engine_mode(struct lp5521_engine *engine, u8 mode)
  141. {
  142. struct lp5521_chip *chip = engine_to_lp5521(engine);
  143. struct i2c_client *client = chip->client;
  144. int ret;
  145. u8 engine_state;
  146. /* Only transition between RUN and DIRECT mode are handled here */
  147. if (mode == LP5521_CMD_LOAD)
  148. return 0;
  149. if (mode == LP5521_CMD_DISABLED)
  150. mode = LP5521_CMD_DIRECT;
  151. ret = lp5521_read(client, LP5521_REG_OP_MODE, &engine_state);
  152. /* set mode only for this engine */
  153. engine_state &= ~(engine->engine_mask);
  154. mode &= engine->engine_mask;
  155. engine_state |= mode;
  156. ret |= lp5521_write(client, LP5521_REG_OP_MODE, engine_state);
  157. return ret;
  158. }
  159. static int lp5521_load_program(struct lp5521_engine *eng, const u8 *pattern)
  160. {
  161. struct lp5521_chip *chip = engine_to_lp5521(eng);
  162. struct i2c_client *client = chip->client;
  163. int ret;
  164. int addr;
  165. u8 mode;
  166. /* move current engine to direct mode and remember the state */
  167. ret = lp5521_set_engine_mode(eng, LP5521_CMD_DIRECT);
  168. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  169. usleep_range(1000, 2000);
  170. ret |= lp5521_read(client, LP5521_REG_OP_MODE, &mode);
  171. /* For loading, all the engines to load mode */
  172. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT);
  173. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  174. usleep_range(1000, 2000);
  175. lp5521_write(client, LP5521_REG_OP_MODE, LP5521_CMD_LOAD);
  176. /* Mode change requires min 500 us delay. 1 - 2 ms with margin */
  177. usleep_range(1000, 2000);
  178. addr = LP5521_PROG_MEM_BASE + eng->prog_page * LP5521_PROG_MEM_SIZE;
  179. i2c_smbus_write_i2c_block_data(client,
  180. addr,
  181. LP5521_PROG_MEM_SIZE,
  182. pattern);
  183. ret |= lp5521_write(client, LP5521_REG_OP_MODE, mode);
  184. return ret;
  185. }
  186. static int lp5521_set_led_current(struct lp5521_chip *chip, int led, u8 curr)
  187. {
  188. return lp5521_write(chip->client,
  189. LP5521_REG_LED_CURRENT_BASE + chip->leds[led].chan_nr,
  190. curr);
  191. }
  192. static void lp5521_init_engine(struct lp5521_chip *chip,
  193. const struct attribute_group *attr_group)
  194. {
  195. int i;
  196. for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
  197. chip->engines[i].id = i + 1;
  198. chip->engines[i].engine_mask = LP5521_ENG_MASK_BASE >> (i * 2);
  199. chip->engines[i].prog_page = i;
  200. chip->engines[i].attributes = &attr_group[i];
  201. }
  202. }
  203. static int lp5521_configure(struct i2c_client *client,
  204. const struct attribute_group *attr_group)
  205. {
  206. struct lp5521_chip *chip = i2c_get_clientdata(client);
  207. int ret;
  208. lp5521_init_engine(chip, attr_group);
  209. lp5521_write(client, LP5521_REG_RESET, 0xff);
  210. usleep_range(10000, 20000); /*
  211. * Exact value is not available. 10 - 20ms
  212. * appears to be enough for reset.
  213. */
  214. /* Set all PWMs to direct control mode */
  215. ret = lp5521_write(client, LP5521_REG_OP_MODE, 0x3F);
  216. /* Enable auto-powersave, set charge pump to auto, red to battery */
  217. ret |= lp5521_write(client, LP5521_REG_CONFIG,
  218. LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT);
  219. /* Initialize all channels PWM to zero -> leds off */
  220. ret |= lp5521_write(client, LP5521_REG_R_PWM, 0);
  221. ret |= lp5521_write(client, LP5521_REG_G_PWM, 0);
  222. ret |= lp5521_write(client, LP5521_REG_B_PWM, 0);
  223. /* Set engines are set to run state when OP_MODE enables engines */
  224. ret |= lp5521_write(client, LP5521_REG_ENABLE,
  225. LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM |
  226. LP5521_EXEC_RUN);
  227. /* enable takes 500us. 1 - 2 ms leaves some margin */
  228. usleep_range(1000, 2000);
  229. return ret;
  230. }
  231. static int lp5521_run_selftest(struct lp5521_chip *chip, char *buf)
  232. {
  233. int ret;
  234. u8 status;
  235. ret = lp5521_read(chip->client, LP5521_REG_STATUS, &status);
  236. if (ret < 0)
  237. return ret;
  238. /* Check that ext clock is really in use if requested */
  239. if (chip->pdata && chip->pdata->clock_mode == LP5521_CLOCK_EXT)
  240. if ((status & LP5521_EXT_CLK_USED) == 0)
  241. return -EIO;
  242. return 0;
  243. }
  244. static void lp5521_set_brightness(struct led_classdev *cdev,
  245. enum led_brightness brightness)
  246. {
  247. struct lp5521_led *led = cdev_to_led(cdev);
  248. led->brightness = (u8)brightness;
  249. schedule_work(&led->brightness_work);
  250. }
  251. static void lp5521_led_brightness_work(struct work_struct *work)
  252. {
  253. struct lp5521_led *led = container_of(work,
  254. struct lp5521_led,
  255. brightness_work);
  256. struct lp5521_chip *chip = led_to_lp5521(led);
  257. struct i2c_client *client = chip->client;
  258. mutex_lock(&chip->lock);
  259. lp5521_write(client, LP5521_REG_LED_PWM_BASE + led->chan_nr,
  260. led->brightness);
  261. mutex_unlock(&chip->lock);
  262. }
  263. /* Detect the chip by setting its ENABLE register and reading it back. */
  264. static int lp5521_detect(struct i2c_client *client)
  265. {
  266. int ret;
  267. u8 buf;
  268. ret = lp5521_write(client, LP5521_REG_ENABLE,
  269. LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM);
  270. if (ret)
  271. return ret;
  272. /* enable takes 500us. 1 - 2 ms leaves some margin */
  273. usleep_range(1000, 2000);
  274. ret = lp5521_read(client, LP5521_REG_ENABLE, &buf);
  275. if (ret)
  276. return ret;
  277. if (buf != (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM))
  278. return -ENODEV;
  279. return 0;
  280. }
  281. /* Set engine mode and create appropriate sysfs attributes, if required. */
  282. static int lp5521_set_mode(struct lp5521_engine *engine, u8 mode)
  283. {
  284. struct lp5521_chip *chip = engine_to_lp5521(engine);
  285. struct i2c_client *client = chip->client;
  286. struct device *dev = &client->dev;
  287. int ret = 0;
  288. /* if in that mode already do nothing, except for run */
  289. if (mode == engine->mode && mode != LP5521_CMD_RUN)
  290. return 0;
  291. if (mode == LP5521_CMD_RUN) {
  292. ret = lp5521_set_engine_mode(engine, LP5521_CMD_RUN);
  293. } else if (mode == LP5521_CMD_LOAD) {
  294. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  295. lp5521_set_engine_mode(engine, LP5521_CMD_LOAD);
  296. ret = sysfs_create_group(&dev->kobj, engine->attributes);
  297. if (ret)
  298. return ret;
  299. } else if (mode == LP5521_CMD_DISABLED) {
  300. lp5521_set_engine_mode(engine, LP5521_CMD_DISABLED);
  301. }
  302. /* remove load attribute from sysfs if not in load mode */
  303. if (engine->mode == LP5521_CMD_LOAD && mode != LP5521_CMD_LOAD)
  304. sysfs_remove_group(&dev->kobj, engine->attributes);
  305. engine->mode = mode;
  306. return ret;
  307. }
  308. static int lp5521_do_store_load(struct lp5521_engine *engine,
  309. const char *buf, size_t len)
  310. {
  311. struct lp5521_chip *chip = engine_to_lp5521(engine);
  312. struct i2c_client *client = chip->client;
  313. int ret, nrchars, offset = 0, i = 0;
  314. char c[3];
  315. unsigned cmd;
  316. u8 pattern[LP5521_PROGRAM_LENGTH] = {0};
  317. while ((offset < len - 1) && (i < LP5521_PROGRAM_LENGTH)) {
  318. /* separate sscanfs because length is working only for %s */
  319. ret = sscanf(buf + offset, "%2s%n ", c, &nrchars);
  320. ret = sscanf(c, "%2x", &cmd);
  321. if (ret != 1)
  322. goto fail;
  323. pattern[i] = (u8)cmd;
  324. offset += nrchars;
  325. i++;
  326. }
  327. /* Each instruction is 16bit long. Check that length is even */
  328. if (i % 2)
  329. goto fail;
  330. mutex_lock(&chip->lock);
  331. ret = lp5521_load_program(engine, pattern);
  332. mutex_unlock(&chip->lock);
  333. if (ret) {
  334. dev_err(&client->dev, "failed loading pattern\n");
  335. return ret;
  336. }
  337. return len;
  338. fail:
  339. dev_err(&client->dev, "wrong pattern format\n");
  340. return -EINVAL;
  341. }
  342. static ssize_t store_engine_load(struct device *dev,
  343. struct device_attribute *attr,
  344. const char *buf, size_t len, int nr)
  345. {
  346. struct i2c_client *client = to_i2c_client(dev);
  347. struct lp5521_chip *chip = i2c_get_clientdata(client);
  348. return lp5521_do_store_load(&chip->engines[nr - 1], buf, len);
  349. }
  350. #define store_load(nr) \
  351. static ssize_t store_engine##nr##_load(struct device *dev, \
  352. struct device_attribute *attr, \
  353. const char *buf, size_t len) \
  354. { \
  355. return store_engine_load(dev, attr, buf, len, nr); \
  356. }
  357. store_load(1)
  358. store_load(2)
  359. store_load(3)
  360. static ssize_t show_engine_mode(struct device *dev,
  361. struct device_attribute *attr,
  362. char *buf, int nr)
  363. {
  364. struct i2c_client *client = to_i2c_client(dev);
  365. struct lp5521_chip *chip = i2c_get_clientdata(client);
  366. switch (chip->engines[nr - 1].mode) {
  367. case LP5521_CMD_RUN:
  368. return sprintf(buf, "run\n");
  369. case LP5521_CMD_LOAD:
  370. return sprintf(buf, "load\n");
  371. case LP5521_CMD_DISABLED:
  372. return sprintf(buf, "disabled\n");
  373. default:
  374. return sprintf(buf, "disabled\n");
  375. }
  376. }
  377. #define show_mode(nr) \
  378. static ssize_t show_engine##nr##_mode(struct device *dev, \
  379. struct device_attribute *attr, \
  380. char *buf) \
  381. { \
  382. return show_engine_mode(dev, attr, buf, nr); \
  383. }
  384. show_mode(1)
  385. show_mode(2)
  386. show_mode(3)
  387. static ssize_t store_engine_mode(struct device *dev,
  388. struct device_attribute *attr,
  389. const char *buf, size_t len, int nr)
  390. {
  391. struct i2c_client *client = to_i2c_client(dev);
  392. struct lp5521_chip *chip = i2c_get_clientdata(client);
  393. struct lp5521_engine *engine = &chip->engines[nr - 1];
  394. mutex_lock(&chip->lock);
  395. if (!strncmp(buf, "run", 3))
  396. lp5521_set_mode(engine, LP5521_CMD_RUN);
  397. else if (!strncmp(buf, "load", 4))
  398. lp5521_set_mode(engine, LP5521_CMD_LOAD);
  399. else if (!strncmp(buf, "disabled", 8))
  400. lp5521_set_mode(engine, LP5521_CMD_DISABLED);
  401. mutex_unlock(&chip->lock);
  402. return len;
  403. }
  404. #define store_mode(nr) \
  405. static ssize_t store_engine##nr##_mode(struct device *dev, \
  406. struct device_attribute *attr, \
  407. const char *buf, size_t len) \
  408. { \
  409. return store_engine_mode(dev, attr, buf, len, nr); \
  410. }
  411. store_mode(1)
  412. store_mode(2)
  413. store_mode(3)
  414. static ssize_t show_max_current(struct device *dev,
  415. struct device_attribute *attr,
  416. char *buf)
  417. {
  418. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  419. struct lp5521_led *led = cdev_to_led(led_cdev);
  420. return sprintf(buf, "%d\n", led->max_current);
  421. }
  422. static ssize_t show_current(struct device *dev,
  423. struct device_attribute *attr,
  424. char *buf)
  425. {
  426. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  427. struct lp5521_led *led = cdev_to_led(led_cdev);
  428. return sprintf(buf, "%d\n", led->led_current);
  429. }
  430. static ssize_t store_current(struct device *dev,
  431. struct device_attribute *attr,
  432. const char *buf, size_t len)
  433. {
  434. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  435. struct lp5521_led *led = cdev_to_led(led_cdev);
  436. struct lp5521_chip *chip = led_to_lp5521(led);
  437. ssize_t ret;
  438. unsigned long curr;
  439. if (strict_strtoul(buf, 0, &curr))
  440. return -EINVAL;
  441. if (curr > led->max_current)
  442. return -EINVAL;
  443. mutex_lock(&chip->lock);
  444. ret = lp5521_set_led_current(chip, led->id, curr);
  445. mutex_unlock(&chip->lock);
  446. if (ret < 0)
  447. return ret;
  448. led->led_current = (u8)curr;
  449. return len;
  450. }
  451. static ssize_t lp5521_selftest(struct device *dev,
  452. struct device_attribute *attr,
  453. char *buf)
  454. {
  455. struct i2c_client *client = to_i2c_client(dev);
  456. struct lp5521_chip *chip = i2c_get_clientdata(client);
  457. int ret;
  458. mutex_lock(&chip->lock);
  459. ret = lp5521_run_selftest(chip, buf);
  460. mutex_unlock(&chip->lock);
  461. return sprintf(buf, "%s\n", ret ? "FAIL" : "OK");
  462. }
  463. /* led class device attributes */
  464. static DEVICE_ATTR(led_current, S_IRUGO | S_IWUGO, show_current, store_current);
  465. static DEVICE_ATTR(max_current, S_IRUGO , show_max_current, NULL);
  466. static struct attribute *lp5521_led_attributes[] = {
  467. &dev_attr_led_current.attr,
  468. &dev_attr_max_current.attr,
  469. NULL,
  470. };
  471. static struct attribute_group lp5521_led_attribute_group = {
  472. .attrs = lp5521_led_attributes
  473. };
  474. /* device attributes */
  475. static DEVICE_ATTR(engine1_mode, S_IRUGO | S_IWUGO,
  476. show_engine1_mode, store_engine1_mode);
  477. static DEVICE_ATTR(engine2_mode, S_IRUGO | S_IWUGO,
  478. show_engine2_mode, store_engine2_mode);
  479. static DEVICE_ATTR(engine3_mode, S_IRUGO | S_IWUGO,
  480. show_engine3_mode, store_engine3_mode);
  481. static DEVICE_ATTR(engine1_load, S_IWUGO, NULL, store_engine1_load);
  482. static DEVICE_ATTR(engine2_load, S_IWUGO, NULL, store_engine2_load);
  483. static DEVICE_ATTR(engine3_load, S_IWUGO, NULL, store_engine3_load);
  484. static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL);
  485. static struct attribute *lp5521_attributes[] = {
  486. &dev_attr_engine1_mode.attr,
  487. &dev_attr_engine2_mode.attr,
  488. &dev_attr_engine3_mode.attr,
  489. &dev_attr_selftest.attr,
  490. NULL
  491. };
  492. static struct attribute *lp5521_engine1_attributes[] = {
  493. &dev_attr_engine1_load.attr,
  494. NULL
  495. };
  496. static struct attribute *lp5521_engine2_attributes[] = {
  497. &dev_attr_engine2_load.attr,
  498. NULL
  499. };
  500. static struct attribute *lp5521_engine3_attributes[] = {
  501. &dev_attr_engine3_load.attr,
  502. NULL
  503. };
  504. static const struct attribute_group lp5521_group = {
  505. .attrs = lp5521_attributes,
  506. };
  507. static const struct attribute_group lp5521_engine_group[] = {
  508. {.attrs = lp5521_engine1_attributes },
  509. {.attrs = lp5521_engine2_attributes },
  510. {.attrs = lp5521_engine3_attributes },
  511. };
  512. static int lp5521_register_sysfs(struct i2c_client *client)
  513. {
  514. struct device *dev = &client->dev;
  515. return sysfs_create_group(&dev->kobj, &lp5521_group);
  516. }
  517. static void lp5521_unregister_sysfs(struct i2c_client *client)
  518. {
  519. struct lp5521_chip *chip = i2c_get_clientdata(client);
  520. struct device *dev = &client->dev;
  521. int i;
  522. sysfs_remove_group(&dev->kobj, &lp5521_group);
  523. for (i = 0; i < ARRAY_SIZE(chip->engines); i++) {
  524. if (chip->engines[i].mode == LP5521_CMD_LOAD)
  525. sysfs_remove_group(&dev->kobj,
  526. chip->engines[i].attributes);
  527. }
  528. for (i = 0; i < chip->num_leds; i++)
  529. sysfs_remove_group(&chip->leds[i].cdev.dev->kobj,
  530. &lp5521_led_attribute_group);
  531. }
  532. static int __init lp5521_init_led(struct lp5521_led *led,
  533. struct i2c_client *client,
  534. int chan, struct lp5521_platform_data *pdata)
  535. {
  536. struct device *dev = &client->dev;
  537. char name[32];
  538. int res;
  539. if (chan >= LP5521_MAX_LEDS)
  540. return -EINVAL;
  541. if (pdata->led_config[chan].led_current == 0)
  542. return 0;
  543. led->led_current = pdata->led_config[chan].led_current;
  544. led->max_current = pdata->led_config[chan].max_current;
  545. led->chan_nr = pdata->led_config[chan].chan_nr;
  546. if (led->chan_nr >= LP5521_MAX_LEDS) {
  547. dev_err(dev, "Use channel numbers between 0 and %d\n",
  548. LP5521_MAX_LEDS - 1);
  549. return -EINVAL;
  550. }
  551. snprintf(name, sizeof(name), "%s:channel%d", client->name, chan);
  552. led->cdev.brightness_set = lp5521_set_brightness;
  553. led->cdev.name = name;
  554. res = led_classdev_register(dev, &led->cdev);
  555. if (res < 0) {
  556. dev_err(dev, "couldn't register led on channel %d\n", chan);
  557. return res;
  558. }
  559. res = sysfs_create_group(&led->cdev.dev->kobj,
  560. &lp5521_led_attribute_group);
  561. if (res < 0) {
  562. dev_err(dev, "couldn't register current attribute\n");
  563. led_classdev_unregister(&led->cdev);
  564. return res;
  565. }
  566. return 0;
  567. }
  568. static int lp5521_probe(struct i2c_client *client,
  569. const struct i2c_device_id *id)
  570. {
  571. struct lp5521_chip *chip;
  572. struct lp5521_platform_data *pdata;
  573. int ret, i, led;
  574. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  575. if (!chip)
  576. return -ENOMEM;
  577. i2c_set_clientdata(client, chip);
  578. chip->client = client;
  579. pdata = client->dev.platform_data;
  580. if (!pdata) {
  581. dev_err(&client->dev, "no platform data\n");
  582. ret = -EINVAL;
  583. goto fail1;
  584. }
  585. mutex_init(&chip->lock);
  586. chip->pdata = pdata;
  587. if (pdata->setup_resources) {
  588. ret = pdata->setup_resources();
  589. if (ret < 0)
  590. goto fail1;
  591. }
  592. if (pdata->enable) {
  593. pdata->enable(0);
  594. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  595. pdata->enable(1);
  596. usleep_range(1000, 2000); /* 500us abs min. */
  597. }
  598. ret = lp5521_detect(client);
  599. if (ret) {
  600. dev_err(&client->dev, "Chip not found\n");
  601. goto fail2;
  602. }
  603. dev_info(&client->dev, "%s programmable led chip found\n", id->name);
  604. ret = lp5521_configure(client, lp5521_engine_group);
  605. if (ret < 0) {
  606. dev_err(&client->dev, "error configuring chip\n");
  607. goto fail2;
  608. }
  609. /* Initialize leds */
  610. chip->num_channels = pdata->num_channels;
  611. chip->num_leds = 0;
  612. led = 0;
  613. for (i = 0; i < pdata->num_channels; i++) {
  614. /* Do not initialize channels that are not connected */
  615. if (pdata->led_config[i].led_current == 0)
  616. continue;
  617. ret = lp5521_init_led(&chip->leds[led], client, i, pdata);
  618. if (ret) {
  619. dev_err(&client->dev, "error initializing leds\n");
  620. goto fail3;
  621. }
  622. chip->num_leds++;
  623. chip->leds[led].id = led;
  624. /* Set initial LED current */
  625. lp5521_set_led_current(chip, led,
  626. chip->leds[led].led_current);
  627. INIT_WORK(&(chip->leds[led].brightness_work),
  628. lp5521_led_brightness_work);
  629. led++;
  630. }
  631. ret = lp5521_register_sysfs(client);
  632. if (ret) {
  633. dev_err(&client->dev, "registering sysfs failed\n");
  634. goto fail3;
  635. }
  636. return ret;
  637. fail3:
  638. for (i = 0; i < chip->num_leds; i++) {
  639. led_classdev_unregister(&chip->leds[i].cdev);
  640. cancel_work_sync(&chip->leds[i].brightness_work);
  641. }
  642. fail2:
  643. if (pdata->enable)
  644. pdata->enable(0);
  645. if (pdata->release_resources)
  646. pdata->release_resources();
  647. fail1:
  648. kfree(chip);
  649. return ret;
  650. }
  651. static int lp5521_remove(struct i2c_client *client)
  652. {
  653. struct lp5521_chip *chip = i2c_get_clientdata(client);
  654. int i;
  655. lp5521_unregister_sysfs(client);
  656. for (i = 0; i < chip->num_leds; i++) {
  657. led_classdev_unregister(&chip->leds[i].cdev);
  658. cancel_work_sync(&chip->leds[i].brightness_work);
  659. }
  660. if (chip->pdata->enable)
  661. chip->pdata->enable(0);
  662. if (chip->pdata->release_resources)
  663. chip->pdata->release_resources();
  664. kfree(chip);
  665. return 0;
  666. }
  667. static const struct i2c_device_id lp5521_id[] = {
  668. { "lp5521", 0 }, /* Three channel chip */
  669. { }
  670. };
  671. MODULE_DEVICE_TABLE(i2c, lp5521_id);
  672. static struct i2c_driver lp5521_driver = {
  673. .driver = {
  674. .name = "lp5521",
  675. },
  676. .probe = lp5521_probe,
  677. .remove = lp5521_remove,
  678. .id_table = lp5521_id,
  679. };
  680. static int __init lp5521_init(void)
  681. {
  682. int ret;
  683. ret = i2c_add_driver(&lp5521_driver);
  684. if (ret < 0)
  685. printk(KERN_ALERT "Adding lp5521 driver failed\n");
  686. return ret;
  687. }
  688. static void __exit lp5521_exit(void)
  689. {
  690. i2c_del_driver(&lp5521_driver);
  691. }
  692. module_init(lp5521_init);
  693. module_exit(lp5521_exit);
  694. MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo");
  695. MODULE_DESCRIPTION("LP5521 LED engine");
  696. MODULE_LICENSE("GPL v2");