leds-blinkm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * leds-blinkm.c
  3. * (c) Jan-Simon Möller (dl9pf@gmx.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/i2c.h>
  23. #include <linux/err.h>
  24. #include <linux/mutex.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/printk.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/leds.h>
  29. #include <linux/delay.h>
  30. /* Addresses to scan - BlinkM is on 0x09 by default*/
  31. static const unsigned short normal_i2c[] = { 0x09, I2C_CLIENT_END };
  32. static int blinkm_transfer_hw(struct i2c_client *client, int cmd);
  33. static int blinkm_test_run(struct i2c_client *client);
  34. struct blinkm_led {
  35. struct i2c_client *i2c_client;
  36. struct led_classdev led_cdev;
  37. int id;
  38. atomic_t active;
  39. };
  40. struct blinkm_work {
  41. struct blinkm_led *blinkm_led;
  42. struct work_struct work;
  43. };
  44. #define cdev_to_blmled(c) container_of(c, struct blinkm_led, led_cdev)
  45. #define work_to_blmwork(c) container_of(c, struct blinkm_work, work)
  46. struct blinkm_data {
  47. struct i2c_client *i2c_client;
  48. struct mutex update_lock;
  49. /* used for led class interface */
  50. struct blinkm_led blinkm_leds[3];
  51. /* used for "blinkm" sysfs interface */
  52. u8 red; /* color red */
  53. u8 green; /* color green */
  54. u8 blue; /* color blue */
  55. /* next values to use for transfer */
  56. u8 next_red; /* color red */
  57. u8 next_green; /* color green */
  58. u8 next_blue; /* color blue */
  59. /* internal use */
  60. u8 args[7]; /* set of args for transmission */
  61. u8 i2c_addr; /* i2c addr */
  62. u8 fw_ver; /* firmware version */
  63. /* used, but not from userspace */
  64. u8 hue; /* HSB hue */
  65. u8 saturation; /* HSB saturation */
  66. u8 brightness; /* HSB brightness */
  67. u8 next_hue; /* HSB hue */
  68. u8 next_saturation; /* HSB saturation */
  69. u8 next_brightness; /* HSB brightness */
  70. /* currently unused / todo */
  71. u8 fade_speed; /* fade speed 1 - 255 */
  72. s8 time_adjust; /* time adjust -128 - 127 */
  73. u8 fade:1; /* fade on = 1, off = 0 */
  74. u8 rand:1; /* rand fade mode on = 1 */
  75. u8 script_id; /* script ID */
  76. u8 script_repeats; /* repeats of script */
  77. u8 script_startline; /* line to start */
  78. };
  79. /* Colors */
  80. #define RED 0
  81. #define GREEN 1
  82. #define BLUE 2
  83. /* mapping command names to cmd chars - see datasheet */
  84. #define BLM_GO_RGB 0
  85. #define BLM_FADE_RGB 1
  86. #define BLM_FADE_HSB 2
  87. #define BLM_FADE_RAND_RGB 3
  88. #define BLM_FADE_RAND_HSB 4
  89. #define BLM_PLAY_SCRIPT 5
  90. #define BLM_STOP_SCRIPT 6
  91. #define BLM_SET_FADE_SPEED 7
  92. #define BLM_SET_TIME_ADJ 8
  93. #define BLM_GET_CUR_RGB 9
  94. #define BLM_WRITE_SCRIPT_LINE 10
  95. #define BLM_READ_SCRIPT_LINE 11
  96. #define BLM_SET_SCRIPT_LR 12 /* Length & Repeats */
  97. #define BLM_SET_ADDR 13
  98. #define BLM_GET_ADDR 14
  99. #define BLM_GET_FW_VER 15
  100. #define BLM_SET_STARTUP_PARAM 16
  101. /* BlinkM Commands
  102. * as extracted out of the datasheet:
  103. *
  104. * cmdchar = command (ascii)
  105. * cmdbyte = command in hex
  106. * nr_args = number of arguments (to send)
  107. * nr_ret = number of return values (to read)
  108. * dir = direction (0 = read, 1 = write, 2 = both)
  109. *
  110. */
  111. static const struct {
  112. char cmdchar;
  113. u8 cmdbyte;
  114. u8 nr_args;
  115. u8 nr_ret;
  116. u8 dir:2;
  117. } blinkm_cmds[17] = {
  118. /* cmdchar, cmdbyte, nr_args, nr_ret, dir */
  119. { 'n', 0x6e, 3, 0, 1},
  120. { 'c', 0x63, 3, 0, 1},
  121. { 'h', 0x68, 3, 0, 1},
  122. { 'C', 0x43, 3, 0, 1},
  123. { 'H', 0x48, 3, 0, 1},
  124. { 'p', 0x70, 3, 0, 1},
  125. { 'o', 0x6f, 0, 0, 1},
  126. { 'f', 0x66, 1, 0, 1},
  127. { 't', 0x74, 1, 0, 1},
  128. { 'g', 0x67, 0, 3, 0},
  129. { 'W', 0x57, 7, 0, 1},
  130. { 'R', 0x52, 2, 5, 2},
  131. { 'L', 0x4c, 3, 0, 1},
  132. { 'A', 0x41, 4, 0, 1},
  133. { 'a', 0x61, 0, 1, 0},
  134. { 'Z', 0x5a, 0, 1, 0},
  135. { 'B', 0x42, 5, 0, 1},
  136. };
  137. static ssize_t show_color_common(struct device *dev, char *buf, int color)
  138. {
  139. struct i2c_client *client;
  140. struct blinkm_data *data;
  141. int ret;
  142. client = to_i2c_client(dev);
  143. data = i2c_get_clientdata(client);
  144. ret = blinkm_transfer_hw(client, BLM_GET_CUR_RGB);
  145. if (ret < 0)
  146. return ret;
  147. switch (color) {
  148. case RED:
  149. return scnprintf(buf, PAGE_SIZE, "%02X\n", data->red);
  150. case GREEN:
  151. return scnprintf(buf, PAGE_SIZE, "%02X\n", data->green);
  152. case BLUE:
  153. return scnprintf(buf, PAGE_SIZE, "%02X\n", data->blue);
  154. default:
  155. return -EINVAL;
  156. }
  157. return -EINVAL;
  158. }
  159. static int store_color_common(struct device *dev, const char *buf, int color)
  160. {
  161. struct i2c_client *client;
  162. struct blinkm_data *data;
  163. int ret;
  164. u8 value;
  165. client = to_i2c_client(dev);
  166. data = i2c_get_clientdata(client);
  167. ret = kstrtou8(buf, 10, &value);
  168. if (ret < 0) {
  169. dev_err(dev, "BlinkM: value too large!\n");
  170. return ret;
  171. }
  172. switch (color) {
  173. case RED:
  174. data->next_red = value;
  175. break;
  176. case GREEN:
  177. data->next_green = value;
  178. break;
  179. case BLUE:
  180. data->next_blue = value;
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. dev_dbg(dev, "next_red = %d, next_green = %d, next_blue = %d\n",
  186. data->next_red, data->next_green, data->next_blue);
  187. /* if mode ... */
  188. ret = blinkm_transfer_hw(client, BLM_GO_RGB);
  189. if (ret < 0) {
  190. dev_err(dev, "BlinkM: can't set RGB\n");
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. static ssize_t show_red(struct device *dev, struct device_attribute *attr,
  196. char *buf)
  197. {
  198. return show_color_common(dev, buf, RED);
  199. }
  200. static ssize_t store_red(struct device *dev, struct device_attribute *attr,
  201. const char *buf, size_t count)
  202. {
  203. int ret;
  204. ret = store_color_common(dev, buf, RED);
  205. if (ret < 0)
  206. return ret;
  207. return count;
  208. }
  209. static DEVICE_ATTR(red, S_IRUGO | S_IWUSR, show_red, store_red);
  210. static ssize_t show_green(struct device *dev, struct device_attribute *attr,
  211. char *buf)
  212. {
  213. return show_color_common(dev, buf, GREEN);
  214. }
  215. static ssize_t store_green(struct device *dev, struct device_attribute *attr,
  216. const char *buf, size_t count)
  217. {
  218. int ret;
  219. ret = store_color_common(dev, buf, GREEN);
  220. if (ret < 0)
  221. return ret;
  222. return count;
  223. }
  224. static DEVICE_ATTR(green, S_IRUGO | S_IWUSR, show_green, store_green);
  225. static ssize_t show_blue(struct device *dev, struct device_attribute *attr,
  226. char *buf)
  227. {
  228. return show_color_common(dev, buf, BLUE);
  229. }
  230. static ssize_t store_blue(struct device *dev, struct device_attribute *attr,
  231. const char *buf, size_t count)
  232. {
  233. int ret;
  234. ret = store_color_common(dev, buf, BLUE);
  235. if (ret < 0)
  236. return ret;
  237. return count;
  238. }
  239. static DEVICE_ATTR(blue, S_IRUGO | S_IWUSR, show_blue, store_blue);
  240. static ssize_t show_test(struct device *dev, struct device_attribute *attr,
  241. char *buf)
  242. {
  243. return scnprintf(buf, PAGE_SIZE,
  244. "#Write into test to start test sequence!#\n");
  245. }
  246. static ssize_t store_test(struct device *dev, struct device_attribute *attr,
  247. const char *buf, size_t count)
  248. {
  249. struct i2c_client *client;
  250. int ret;
  251. client = to_i2c_client(dev);
  252. /*test */
  253. ret = blinkm_test_run(client);
  254. if (ret < 0)
  255. return ret;
  256. return count;
  257. }
  258. static DEVICE_ATTR(test, S_IRUGO | S_IWUSR, show_test, store_test);
  259. /* TODO: HSB, fade, timeadj, script ... */
  260. static struct attribute *blinkm_attrs[] = {
  261. &dev_attr_red.attr,
  262. &dev_attr_green.attr,
  263. &dev_attr_blue.attr,
  264. &dev_attr_test.attr,
  265. NULL,
  266. };
  267. static struct attribute_group blinkm_group = {
  268. .name = "blinkm",
  269. .attrs = blinkm_attrs,
  270. };
  271. static int blinkm_write(struct i2c_client *client, int cmd, u8 *arg)
  272. {
  273. int result;
  274. int i;
  275. int arglen = blinkm_cmds[cmd].nr_args;
  276. /* write out cmd to blinkm - always / default step */
  277. result = i2c_smbus_write_byte(client, blinkm_cmds[cmd].cmdbyte);
  278. if (result < 0)
  279. return result;
  280. /* no args to write out */
  281. if (arglen == 0)
  282. return 0;
  283. for (i = 0; i < arglen; i++) {
  284. /* repeat for arglen */
  285. result = i2c_smbus_write_byte(client, arg[i]);
  286. if (result < 0)
  287. return result;
  288. }
  289. return 0;
  290. }
  291. static int blinkm_read(struct i2c_client *client, int cmd, u8 *arg)
  292. {
  293. int result;
  294. int i;
  295. int retlen = blinkm_cmds[cmd].nr_ret;
  296. for (i = 0; i < retlen; i++) {
  297. /* repeat for retlen */
  298. result = i2c_smbus_read_byte(client);
  299. if (result < 0)
  300. return result;
  301. arg[i] = result;
  302. }
  303. return 0;
  304. }
  305. static int blinkm_transfer_hw(struct i2c_client *client, int cmd)
  306. {
  307. /* the protocol is simple but non-standard:
  308. * e.g. cmd 'g' (= 0x67) for "get device address"
  309. * - which defaults to 0x09 - would be the sequence:
  310. * a) write 0x67 to the device (byte write)
  311. * b) read the value (0x09) back right after (byte read)
  312. *
  313. * Watch out for "unfinished" sequences (i.e. not enough reads
  314. * or writes after a command. It will make the blinkM misbehave.
  315. * Sequence is key here.
  316. */
  317. /* args / return are in private data struct */
  318. struct blinkm_data *data = i2c_get_clientdata(client);
  319. /* We start hardware transfers which are not to be
  320. * mixed with other commands. Aquire a lock now. */
  321. if (mutex_lock_interruptible(&data->update_lock) < 0)
  322. return -EAGAIN;
  323. /* switch cmd - usually write before reads */
  324. switch (cmd) {
  325. case BLM_FADE_RAND_RGB:
  326. case BLM_GO_RGB:
  327. case BLM_FADE_RGB:
  328. data->args[0] = data->next_red;
  329. data->args[1] = data->next_green;
  330. data->args[2] = data->next_blue;
  331. blinkm_write(client, cmd, data->args);
  332. data->red = data->args[0];
  333. data->green = data->args[1];
  334. data->blue = data->args[2];
  335. break;
  336. case BLM_FADE_HSB:
  337. case BLM_FADE_RAND_HSB:
  338. data->args[0] = data->next_hue;
  339. data->args[1] = data->next_saturation;
  340. data->args[2] = data->next_brightness;
  341. blinkm_write(client, cmd, data->args);
  342. data->hue = data->next_hue;
  343. data->saturation = data->next_saturation;
  344. data->brightness = data->next_brightness;
  345. break;
  346. case BLM_PLAY_SCRIPT:
  347. data->args[0] = data->script_id;
  348. data->args[1] = data->script_repeats;
  349. data->args[2] = data->script_startline;
  350. blinkm_write(client, cmd, data->args);
  351. break;
  352. case BLM_STOP_SCRIPT:
  353. blinkm_write(client, cmd, NULL);
  354. break;
  355. case BLM_GET_CUR_RGB:
  356. data->args[0] = data->red;
  357. data->args[1] = data->green;
  358. data->args[2] = data->blue;
  359. blinkm_write(client, cmd, NULL);
  360. blinkm_read(client, cmd, data->args);
  361. data->red = data->args[0];
  362. data->green = data->args[1];
  363. data->blue = data->args[2];
  364. break;
  365. case BLM_GET_ADDR:
  366. data->args[0] = data->i2c_addr;
  367. blinkm_write(client, cmd, NULL);
  368. blinkm_read(client, cmd, data->args);
  369. data->i2c_addr = data->args[0];
  370. break;
  371. case BLM_SET_TIME_ADJ:
  372. case BLM_SET_FADE_SPEED:
  373. case BLM_READ_SCRIPT_LINE:
  374. case BLM_WRITE_SCRIPT_LINE:
  375. case BLM_SET_SCRIPT_LR:
  376. case BLM_SET_ADDR:
  377. case BLM_GET_FW_VER:
  378. case BLM_SET_STARTUP_PARAM:
  379. dev_err(&client->dev,
  380. "BlinkM: cmd %d not implemented yet.\n", cmd);
  381. break;
  382. default:
  383. dev_err(&client->dev, "BlinkM: unknown command %d\n", cmd);
  384. mutex_unlock(&data->update_lock);
  385. return -EINVAL;
  386. } /* end switch(cmd) */
  387. /* transfers done, unlock */
  388. mutex_unlock(&data->update_lock);
  389. return 0;
  390. }
  391. static void led_work(struct work_struct *work)
  392. {
  393. int ret;
  394. struct blinkm_led *led;
  395. struct blinkm_data *data;
  396. struct blinkm_work *blm_work = work_to_blmwork(work);
  397. led = blm_work->blinkm_led;
  398. data = i2c_get_clientdata(led->i2c_client);
  399. ret = blinkm_transfer_hw(led->i2c_client, BLM_GO_RGB);
  400. atomic_dec(&led->active);
  401. dev_dbg(&led->i2c_client->dev,
  402. "# DONE # next_red = %d, next_green = %d,"
  403. " next_blue = %d, active = %d\n",
  404. data->next_red, data->next_green,
  405. data->next_blue, atomic_read(&led->active));
  406. kfree(blm_work);
  407. }
  408. static int blinkm_led_common_set(struct led_classdev *led_cdev,
  409. enum led_brightness value, int color)
  410. {
  411. /* led_brightness is 0, 127 or 255 - we just use it here as-is */
  412. struct blinkm_led *led = cdev_to_blmled(led_cdev);
  413. struct blinkm_data *data = i2c_get_clientdata(led->i2c_client);
  414. struct blinkm_work *bl_work;
  415. switch (color) {
  416. case RED:
  417. /* bail out if there's no change */
  418. if (data->next_red == (u8) value)
  419. return 0;
  420. /* we assume a quite fast sequence here ([off]->on->off)
  421. * think of network led trigger - we cannot blink that fast, so
  422. * in case we already have a off->on->off transition queued up,
  423. * we refuse to queue up more.
  424. * Revisit: fast-changing brightness. */
  425. if (atomic_read(&led->active) > 1)
  426. return 0;
  427. data->next_red = (u8) value;
  428. break;
  429. case GREEN:
  430. /* bail out if there's no change */
  431. if (data->next_green == (u8) value)
  432. return 0;
  433. /* we assume a quite fast sequence here ([off]->on->off)
  434. * Revisit: fast-changing brightness. */
  435. if (atomic_read(&led->active) > 1)
  436. return 0;
  437. data->next_green = (u8) value;
  438. break;
  439. case BLUE:
  440. /* bail out if there's no change */
  441. if (data->next_blue == (u8) value)
  442. return 0;
  443. /* we assume a quite fast sequence here ([off]->on->off)
  444. * Revisit: fast-changing brightness. */
  445. if (atomic_read(&led->active) > 1)
  446. return 0;
  447. data->next_blue = (u8) value;
  448. break;
  449. default:
  450. dev_err(&led->i2c_client->dev, "BlinkM: unknown color.\n");
  451. return -EINVAL;
  452. }
  453. bl_work = kzalloc(sizeof(*bl_work), GFP_ATOMIC);
  454. if (!bl_work)
  455. return -ENOMEM;
  456. atomic_inc(&led->active);
  457. dev_dbg(&led->i2c_client->dev,
  458. "#TO_SCHED# next_red = %d, next_green = %d,"
  459. " next_blue = %d, active = %d\n",
  460. data->next_red, data->next_green,
  461. data->next_blue, atomic_read(&led->active));
  462. /* a fresh work _item_ for each change */
  463. bl_work->blinkm_led = led;
  464. INIT_WORK(&bl_work->work, led_work);
  465. /* queue work in own queue for easy sync on exit*/
  466. schedule_work(&bl_work->work);
  467. return 0;
  468. }
  469. static void blinkm_led_red_set(struct led_classdev *led_cdev,
  470. enum led_brightness value)
  471. {
  472. blinkm_led_common_set(led_cdev, value, RED);
  473. }
  474. static void blinkm_led_green_set(struct led_classdev *led_cdev,
  475. enum led_brightness value)
  476. {
  477. blinkm_led_common_set(led_cdev, value, GREEN);
  478. }
  479. static void blinkm_led_blue_set(struct led_classdev *led_cdev,
  480. enum led_brightness value)
  481. {
  482. blinkm_led_common_set(led_cdev, value, BLUE);
  483. }
  484. static void blinkm_init_hw(struct i2c_client *client)
  485. {
  486. int ret;
  487. ret = blinkm_transfer_hw(client, BLM_STOP_SCRIPT);
  488. ret = blinkm_transfer_hw(client, BLM_GO_RGB);
  489. }
  490. static int blinkm_test_run(struct i2c_client *client)
  491. {
  492. int ret;
  493. struct blinkm_data *data = i2c_get_clientdata(client);
  494. data->next_red = 0x01;
  495. data->next_green = 0x05;
  496. data->next_blue = 0x10;
  497. ret = blinkm_transfer_hw(client, BLM_GO_RGB);
  498. if (ret < 0)
  499. return ret;
  500. msleep(2000);
  501. data->next_red = 0x25;
  502. data->next_green = 0x10;
  503. data->next_blue = 0x31;
  504. ret = blinkm_transfer_hw(client, BLM_FADE_RGB);
  505. if (ret < 0)
  506. return ret;
  507. msleep(2000);
  508. data->next_hue = 0x50;
  509. data->next_saturation = 0x10;
  510. data->next_brightness = 0x20;
  511. ret = blinkm_transfer_hw(client, BLM_FADE_HSB);
  512. if (ret < 0)
  513. return ret;
  514. msleep(2000);
  515. return 0;
  516. }
  517. /* Return 0 if detection is successful, -ENODEV otherwise */
  518. static int blinkm_detect(struct i2c_client *client, struct i2c_board_info *info)
  519. {
  520. struct i2c_adapter *adapter = client->adapter;
  521. int ret;
  522. int count = 99;
  523. u8 tmpargs[7];
  524. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
  525. | I2C_FUNC_SMBUS_WORD_DATA
  526. | I2C_FUNC_SMBUS_WRITE_BYTE))
  527. return -ENODEV;
  528. /* Now, we do the remaining detection. Simple for now. */
  529. /* We might need more guards to protect other i2c slaves */
  530. /* make sure the blinkM is balanced (read/writes) */
  531. while (count > 0) {
  532. ret = blinkm_write(client, BLM_GET_ADDR, NULL);
  533. usleep_range(5000, 10000);
  534. ret = blinkm_read(client, BLM_GET_ADDR, tmpargs);
  535. usleep_range(5000, 10000);
  536. if (tmpargs[0] == 0x09)
  537. count = 0;
  538. count--;
  539. }
  540. /* Step 1: Read BlinkM address back - cmd_char 'a' */
  541. ret = blinkm_write(client, BLM_GET_ADDR, NULL);
  542. if (ret < 0)
  543. return ret;
  544. usleep_range(20000, 30000); /* allow a small delay */
  545. ret = blinkm_read(client, BLM_GET_ADDR, tmpargs);
  546. if (ret < 0)
  547. return ret;
  548. if (tmpargs[0] != 0x09) {
  549. dev_err(&client->dev, "enodev DEV ADDR = 0x%02X\n", tmpargs[0]);
  550. return -ENODEV;
  551. }
  552. strlcpy(info->type, "blinkm", I2C_NAME_SIZE);
  553. return 0;
  554. }
  555. static int blinkm_probe(struct i2c_client *client,
  556. const struct i2c_device_id *id)
  557. {
  558. struct blinkm_data *data;
  559. struct blinkm_led *led[3];
  560. int err, i;
  561. char blinkm_led_name[28];
  562. data = devm_kzalloc(&client->dev,
  563. sizeof(struct blinkm_data), GFP_KERNEL);
  564. if (!data) {
  565. err = -ENOMEM;
  566. goto exit;
  567. }
  568. data->i2c_addr = 0x09;
  569. data->i2c_addr = 0x08;
  570. /* i2c addr - use fake addr of 0x08 initially (real is 0x09) */
  571. data->fw_ver = 0xfe;
  572. /* firmware version - use fake until we read real value
  573. * (currently broken - BlinkM confused!) */
  574. data->script_id = 0x01;
  575. data->i2c_client = client;
  576. i2c_set_clientdata(client, data);
  577. mutex_init(&data->update_lock);
  578. /* Register sysfs hooks */
  579. err = sysfs_create_group(&client->dev.kobj, &blinkm_group);
  580. if (err < 0) {
  581. dev_err(&client->dev, "couldn't register sysfs group\n");
  582. goto exit;
  583. }
  584. for (i = 0; i < 3; i++) {
  585. /* RED = 0, GREEN = 1, BLUE = 2 */
  586. led[i] = &data->blinkm_leds[i];
  587. led[i]->i2c_client = client;
  588. led[i]->id = i;
  589. led[i]->led_cdev.max_brightness = 255;
  590. led[i]->led_cdev.flags = LED_CORE_SUSPENDRESUME;
  591. atomic_set(&led[i]->active, 0);
  592. switch (i) {
  593. case RED:
  594. snprintf(blinkm_led_name, sizeof(blinkm_led_name),
  595. "blinkm-%d-%d-red",
  596. client->adapter->nr,
  597. client->addr);
  598. led[i]->led_cdev.name = blinkm_led_name;
  599. led[i]->led_cdev.brightness_set = blinkm_led_red_set;
  600. err = led_classdev_register(&client->dev,
  601. &led[i]->led_cdev);
  602. if (err < 0) {
  603. dev_err(&client->dev,
  604. "couldn't register LED %s\n",
  605. led[i]->led_cdev.name);
  606. goto failred;
  607. }
  608. break;
  609. case GREEN:
  610. snprintf(blinkm_led_name, sizeof(blinkm_led_name),
  611. "blinkm-%d-%d-green",
  612. client->adapter->nr,
  613. client->addr);
  614. led[i]->led_cdev.name = blinkm_led_name;
  615. led[i]->led_cdev.brightness_set = blinkm_led_green_set;
  616. err = led_classdev_register(&client->dev,
  617. &led[i]->led_cdev);
  618. if (err < 0) {
  619. dev_err(&client->dev,
  620. "couldn't register LED %s\n",
  621. led[i]->led_cdev.name);
  622. goto failgreen;
  623. }
  624. break;
  625. case BLUE:
  626. snprintf(blinkm_led_name, sizeof(blinkm_led_name),
  627. "blinkm-%d-%d-blue",
  628. client->adapter->nr,
  629. client->addr);
  630. led[i]->led_cdev.name = blinkm_led_name;
  631. led[i]->led_cdev.brightness_set = blinkm_led_blue_set;
  632. err = led_classdev_register(&client->dev,
  633. &led[i]->led_cdev);
  634. if (err < 0) {
  635. dev_err(&client->dev,
  636. "couldn't register LED %s\n",
  637. led[i]->led_cdev.name);
  638. goto failblue;
  639. }
  640. break;
  641. } /* end switch */
  642. } /* end for */
  643. /* Initialize the blinkm */
  644. blinkm_init_hw(client);
  645. return 0;
  646. failblue:
  647. led_classdev_unregister(&led[GREEN]->led_cdev);
  648. failgreen:
  649. led_classdev_unregister(&led[RED]->led_cdev);
  650. failred:
  651. sysfs_remove_group(&client->dev.kobj, &blinkm_group);
  652. exit:
  653. return err;
  654. }
  655. static int blinkm_remove(struct i2c_client *client)
  656. {
  657. struct blinkm_data *data = i2c_get_clientdata(client);
  658. int ret = 0;
  659. int i;
  660. /* make sure no workqueue entries are pending */
  661. for (i = 0; i < 3; i++) {
  662. flush_scheduled_work();
  663. led_classdev_unregister(&data->blinkm_leds[i].led_cdev);
  664. }
  665. /* reset rgb */
  666. data->next_red = 0x00;
  667. data->next_green = 0x00;
  668. data->next_blue = 0x00;
  669. ret = blinkm_transfer_hw(client, BLM_FADE_RGB);
  670. if (ret < 0)
  671. dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
  672. /* reset hsb */
  673. data->next_hue = 0x00;
  674. data->next_saturation = 0x00;
  675. data->next_brightness = 0x00;
  676. ret = blinkm_transfer_hw(client, BLM_FADE_HSB);
  677. if (ret < 0)
  678. dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
  679. /* red fade to off */
  680. data->next_red = 0xff;
  681. ret = blinkm_transfer_hw(client, BLM_GO_RGB);
  682. if (ret < 0)
  683. dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
  684. /* off */
  685. data->next_red = 0x00;
  686. ret = blinkm_transfer_hw(client, BLM_FADE_RGB);
  687. if (ret < 0)
  688. dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
  689. sysfs_remove_group(&client->dev.kobj, &blinkm_group);
  690. return 0;
  691. }
  692. static const struct i2c_device_id blinkm_id[] = {
  693. {"blinkm", 0},
  694. {}
  695. };
  696. MODULE_DEVICE_TABLE(i2c, blinkm_id);
  697. /* This is the driver that will be inserted */
  698. static struct i2c_driver blinkm_driver = {
  699. .class = I2C_CLASS_HWMON,
  700. .driver = {
  701. .name = "blinkm",
  702. },
  703. .probe = blinkm_probe,
  704. .remove = blinkm_remove,
  705. .id_table = blinkm_id,
  706. .detect = blinkm_detect,
  707. .address_list = normal_i2c,
  708. };
  709. module_i2c_driver(blinkm_driver);
  710. MODULE_AUTHOR("Jan-Simon Moeller <dl9pf@gmx.de>");
  711. MODULE_DESCRIPTION("BlinkM RGB LED driver");
  712. MODULE_LICENSE("GPL");