1 // SPDX-License-Identifier: GPL-2.0-or-later
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/jiffies.h>
11 #include <linux/i2c.h>
12 #include <linux/err.h>
13 #include <linux/mutex.h>
14 #include <linux/sysfs.h>
15 #include <linux/printk.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/leds.h>
18 #include <linux/delay.h>
19 #include <linux/led-class-multicolor.h>
20 #include <linux/kconfig.h>
24 /* Addresses to scan - BlinkM is on 0x09 by default*/
25 static const unsigned short normal_i2c[] = { 0x09, I2C_CLIENT_END };
27 static int blinkm_transfer_hw(struct i2c_client *client, int cmd);
28 static int blinkm_test_run(struct i2c_client *client);
30 /* Contains structs for both the color-separated sysfs classes, and the new multicolor class */
32 struct i2c_client *i2c_client;
34 /* used when multicolor support is disabled */
35 struct led_classdev led_cdev;
36 struct led_classdev_mc mcled_cdev;
41 #define led_cdev_to_blmled(c) container_of(c, struct blinkm_led, cdev.led_cdev)
42 #define mcled_cdev_to_led(c) container_of(c, struct blinkm_led, cdev.mcled_cdev)
45 struct i2c_client *i2c_client;
46 struct mutex update_lock;
47 /* used for led class interface */
48 struct blinkm_led blinkm_leds[NUM_LEDS];
49 /* used for "blinkm" sysfs interface */
50 u8 red; /* color red */
51 u8 green; /* color green */
52 u8 blue; /* color blue */
53 /* next values to use for transfer */
54 u8 next_red; /* color red */
55 u8 next_green; /* color green */
56 u8 next_blue; /* color blue */
58 u8 args[7]; /* set of args for transmission */
59 u8 i2c_addr; /* i2c addr */
60 u8 fw_ver; /* firmware version */
61 /* used, but not from userspace */
63 u8 saturation; /* HSB saturation */
64 u8 brightness; /* HSB brightness */
65 u8 next_hue; /* HSB hue */
66 u8 next_saturation; /* HSB saturation */
67 u8 next_brightness; /* HSB brightness */
68 /* currently unused / todo */
69 u8 fade_speed; /* fade speed 1 - 255 */
70 s8 time_adjust; /* time adjust -128 - 127 */
71 u8 fade:1; /* fade on = 1, off = 0 */
72 u8 rand:1; /* rand fade mode on = 1 */
73 u8 script_id; /* script ID */
74 u8 script_repeats; /* repeats of script */
75 u8 script_startline; /* line to start */
83 /* mapping command names to cmd chars - see datasheet */
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
103 * as extracted out of the datasheet:
105 * cmdchar = command (ascii)
106 * cmdbyte = command in hex
107 * nr_args = number of arguments (to send)
108 * nr_ret = number of return values (to read)
109 * dir = direction (0 = read, 1 = write, 2 = both)
112 static const struct {
118 } blinkm_cmds[17] = {
119 /* cmdchar, cmdbyte, nr_args, nr_ret, dir */
120 { 'n', 0x6e, 3, 0, 1},
121 { 'c', 0x63, 3, 0, 1},
122 { 'h', 0x68, 3, 0, 1},
123 { 'C', 0x43, 3, 0, 1},
124 { 'H', 0x48, 3, 0, 1},
125 { 'p', 0x70, 3, 0, 1},
126 { 'o', 0x6f, 0, 0, 1},
127 { 'f', 0x66, 1, 0, 1},
128 { 't', 0x74, 1, 0, 1},
129 { 'g', 0x67, 0, 3, 0},
130 { 'W', 0x57, 7, 0, 1},
131 { 'R', 0x52, 2, 5, 2},
132 { 'L', 0x4c, 3, 0, 1},
133 { 'A', 0x41, 4, 0, 1},
134 { 'a', 0x61, 0, 1, 0},
135 { 'Z', 0x5a, 0, 1, 0},
136 { 'B', 0x42, 5, 0, 1},
139 static ssize_t show_color_common(struct device *dev, char *buf, int color)
141 struct i2c_client *client;
142 struct blinkm_data *data;
145 client = to_i2c_client(dev);
146 data = i2c_get_clientdata(client);
148 ret = blinkm_transfer_hw(client, BLM_GET_CUR_RGB);
153 return sysfs_emit(buf, "%02X\n", data->red);
155 return sysfs_emit(buf, "%02X\n", data->green);
157 return sysfs_emit(buf, "%02X\n", data->blue);
164 static int store_color_common(struct device *dev, const char *buf, int color)
166 struct i2c_client *client;
167 struct blinkm_data *data;
171 client = to_i2c_client(dev);
172 data = i2c_get_clientdata(client);
174 ret = kstrtou8(buf, 10, &value);
176 dev_err(dev, "BlinkM: value too large!\n");
182 data->next_red = value;
185 data->next_green = value;
188 data->next_blue = value;
194 dev_dbg(dev, "next_red = %d, next_green = %d, next_blue = %d\n",
195 data->next_red, data->next_green, data->next_blue);
198 ret = blinkm_transfer_hw(client, BLM_GO_RGB);
200 dev_err(dev, "BlinkM: can't set RGB\n");
206 static ssize_t red_show(struct device *dev, struct device_attribute *attr,
209 return show_color_common(dev, buf, RED);
212 static ssize_t red_store(struct device *dev, struct device_attribute *attr,
213 const char *buf, size_t count)
217 ret = store_color_common(dev, buf, RED);
223 static DEVICE_ATTR_RW(red);
225 static ssize_t green_show(struct device *dev, struct device_attribute *attr,
228 return show_color_common(dev, buf, GREEN);
231 static ssize_t green_store(struct device *dev, struct device_attribute *attr,
232 const char *buf, size_t count)
237 ret = store_color_common(dev, buf, GREEN);
243 static DEVICE_ATTR_RW(green);
245 static ssize_t blue_show(struct device *dev, struct device_attribute *attr,
248 return show_color_common(dev, buf, BLUE);
251 static ssize_t blue_store(struct device *dev, struct device_attribute *attr,
252 const char *buf, size_t count)
256 ret = store_color_common(dev, buf, BLUE);
262 static DEVICE_ATTR_RW(blue);
264 static ssize_t test_show(struct device *dev, struct device_attribute *attr,
267 return sysfs_emit(buf,
268 "#Write into test to start test sequence!#\n");
271 static ssize_t test_store(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
275 struct i2c_client *client;
277 client = to_i2c_client(dev);
280 ret = blinkm_test_run(client);
287 static DEVICE_ATTR_RW(test);
289 /* TODO: HSB, fade, timeadj, script ... */
291 static struct attribute *blinkm_attrs[] = {
293 &dev_attr_green.attr,
299 static const struct attribute_group blinkm_group = {
301 .attrs = blinkm_attrs,
304 static int blinkm_write(struct i2c_client *client, int cmd, u8 *arg)
308 int arglen = blinkm_cmds[cmd].nr_args;
309 /* write out cmd to blinkm - always / default step */
310 result = i2c_smbus_write_byte(client, blinkm_cmds[cmd].cmdbyte);
313 /* no args to write out */
317 for (i = 0; i < arglen; i++) {
318 /* repeat for arglen */
319 result = i2c_smbus_write_byte(client, arg[i]);
326 static int blinkm_read(struct i2c_client *client, int cmd, u8 *arg)
330 int retlen = blinkm_cmds[cmd].nr_ret;
331 for (i = 0; i < retlen; i++) {
332 /* repeat for retlen */
333 result = i2c_smbus_read_byte(client);
342 static int blinkm_transfer_hw(struct i2c_client *client, int cmd)
344 /* the protocol is simple but non-standard:
345 * e.g. cmd 'g' (= 0x67) for "get device address"
346 * - which defaults to 0x09 - would be the sequence:
347 * a) write 0x67 to the device (byte write)
348 * b) read the value (0x09) back right after (byte read)
350 * Watch out for "unfinished" sequences (i.e. not enough reads
351 * or writes after a command. It will make the blinkM misbehave.
352 * Sequence is key here.
355 /* args / return are in private data struct */
356 struct blinkm_data *data = i2c_get_clientdata(client);
358 /* We start hardware transfers which are not to be
359 * mixed with other commands. Aquire a lock now. */
360 if (mutex_lock_interruptible(&data->update_lock) < 0)
363 /* switch cmd - usually write before reads */
365 case BLM_FADE_RAND_RGB:
368 data->args[0] = data->next_red;
369 data->args[1] = data->next_green;
370 data->args[2] = data->next_blue;
371 blinkm_write(client, cmd, data->args);
372 data->red = data->args[0];
373 data->green = data->args[1];
374 data->blue = data->args[2];
377 case BLM_FADE_RAND_HSB:
378 data->args[0] = data->next_hue;
379 data->args[1] = data->next_saturation;
380 data->args[2] = data->next_brightness;
381 blinkm_write(client, cmd, data->args);
382 data->hue = data->next_hue;
383 data->saturation = data->next_saturation;
384 data->brightness = data->next_brightness;
386 case BLM_PLAY_SCRIPT:
387 data->args[0] = data->script_id;
388 data->args[1] = data->script_repeats;
389 data->args[2] = data->script_startline;
390 blinkm_write(client, cmd, data->args);
392 case BLM_STOP_SCRIPT:
393 blinkm_write(client, cmd, NULL);
395 case BLM_GET_CUR_RGB:
396 data->args[0] = data->red;
397 data->args[1] = data->green;
398 data->args[2] = data->blue;
399 blinkm_write(client, cmd, NULL);
400 blinkm_read(client, cmd, data->args);
401 data->red = data->args[0];
402 data->green = data->args[1];
403 data->blue = data->args[2];
406 data->args[0] = data->i2c_addr;
407 blinkm_write(client, cmd, NULL);
408 blinkm_read(client, cmd, data->args);
409 data->i2c_addr = data->args[0];
411 case BLM_SET_TIME_ADJ:
412 case BLM_SET_FADE_SPEED:
413 case BLM_READ_SCRIPT_LINE:
414 case BLM_WRITE_SCRIPT_LINE:
415 case BLM_SET_SCRIPT_LR:
418 case BLM_SET_STARTUP_PARAM:
419 dev_err(&client->dev,
420 "BlinkM: cmd %d not implemented yet.\n", cmd);
423 dev_err(&client->dev, "BlinkM: unknown command %d\n", cmd);
424 mutex_unlock(&data->update_lock);
426 } /* end switch(cmd) */
428 /* transfers done, unlock */
429 mutex_unlock(&data->update_lock);
433 static int blinkm_set_mc_brightness(struct led_classdev *led_cdev,
434 enum led_brightness value)
436 struct led_classdev_mc *mcled_cdev = lcdev_to_mccdev(led_cdev);
437 struct blinkm_led *led = mcled_cdev_to_led(mcled_cdev);
438 struct blinkm_data *data = i2c_get_clientdata(led->i2c_client);
440 led_mc_calc_color_components(mcled_cdev, value);
442 data->next_red = (u8) mcled_cdev->subled_info[RED].brightness;
443 data->next_green = (u8) mcled_cdev->subled_info[GREEN].brightness;
444 data->next_blue = (u8) mcled_cdev->subled_info[BLUE].brightness;
446 blinkm_transfer_hw(led->i2c_client, BLM_GO_RGB);
451 static int blinkm_led_common_set(struct led_classdev *led_cdev,
452 enum led_brightness value, int color)
454 /* led_brightness is 0, 127 or 255 - we just use it here as-is */
455 struct blinkm_led *led = led_cdev_to_blmled(led_cdev);
456 struct blinkm_data *data = i2c_get_clientdata(led->i2c_client);
460 /* bail out if there's no change */
461 if (data->next_red == (u8) value)
463 data->next_red = (u8) value;
466 /* bail out if there's no change */
467 if (data->next_green == (u8) value)
469 data->next_green = (u8) value;
472 /* bail out if there's no change */
473 if (data->next_blue == (u8) value)
475 data->next_blue = (u8) value;
479 dev_err(&led->i2c_client->dev, "BlinkM: unknown color.\n");
483 blinkm_transfer_hw(led->i2c_client, BLM_GO_RGB);
484 dev_dbg(&led->i2c_client->dev,
485 "# DONE # next_red = %d, next_green = %d,"
487 data->next_red, data->next_green,
492 static int blinkm_led_red_set(struct led_classdev *led_cdev,
493 enum led_brightness value)
495 return blinkm_led_common_set(led_cdev, value, RED);
498 static int blinkm_led_green_set(struct led_classdev *led_cdev,
499 enum led_brightness value)
501 return blinkm_led_common_set(led_cdev, value, GREEN);
504 static int blinkm_led_blue_set(struct led_classdev *led_cdev,
505 enum led_brightness value)
507 return blinkm_led_common_set(led_cdev, value, BLUE);
510 static void blinkm_init_hw(struct i2c_client *client)
512 blinkm_transfer_hw(client, BLM_STOP_SCRIPT);
513 blinkm_transfer_hw(client, BLM_GO_RGB);
516 static int blinkm_test_run(struct i2c_client *client)
519 struct blinkm_data *data = i2c_get_clientdata(client);
521 data->next_red = 0x01;
522 data->next_green = 0x05;
523 data->next_blue = 0x10;
524 ret = blinkm_transfer_hw(client, BLM_GO_RGB);
529 data->next_red = 0x25;
530 data->next_green = 0x10;
531 data->next_blue = 0x31;
532 ret = blinkm_transfer_hw(client, BLM_FADE_RGB);
537 data->next_hue = 0x50;
538 data->next_saturation = 0x10;
539 data->next_brightness = 0x20;
540 ret = blinkm_transfer_hw(client, BLM_FADE_HSB);
548 /* Return 0 if detection is successful, -ENODEV otherwise */
549 static int blinkm_detect(struct i2c_client *client, struct i2c_board_info *info)
551 struct i2c_adapter *adapter = client->adapter;
556 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
557 | I2C_FUNC_SMBUS_WORD_DATA
558 | I2C_FUNC_SMBUS_WRITE_BYTE))
561 /* Now, we do the remaining detection. Simple for now. */
562 /* We might need more guards to protect other i2c slaves */
564 /* make sure the blinkM is balanced (read/writes) */
566 ret = blinkm_write(client, BLM_GET_ADDR, NULL);
569 usleep_range(5000, 10000);
570 ret = blinkm_read(client, BLM_GET_ADDR, tmpargs);
573 usleep_range(5000, 10000);
574 if (tmpargs[0] == 0x09)
579 /* Step 1: Read BlinkM address back - cmd_char 'a' */
580 ret = blinkm_write(client, BLM_GET_ADDR, NULL);
583 usleep_range(20000, 30000); /* allow a small delay */
584 ret = blinkm_read(client, BLM_GET_ADDR, tmpargs);
588 if (tmpargs[0] != 0x09) {
589 dev_err(&client->dev, "enodev DEV ADDR = 0x%02X\n", tmpargs[0]);
593 strscpy(info->type, "blinkm", I2C_NAME_SIZE);
597 static int register_separate_colors(struct i2c_client *client, struct blinkm_data *data)
599 /* 3 separate classes for red, green, and blue respectively */
600 struct blinkm_led *leds[NUM_LEDS];
602 char blinkm_led_name[28];
603 /* Register red, green, and blue sysfs classes */
604 for (int i = 0; i < NUM_LEDS; i++) {
605 /* RED = 0, GREEN = 1, BLUE = 2 */
606 leds[i] = &data->blinkm_leds[i];
607 leds[i]->i2c_client = client;
609 leds[i]->cdev.led_cdev.max_brightness = 255;
610 leds[i]->cdev.led_cdev.flags = LED_CORE_SUSPENDRESUME;
613 scnprintf(blinkm_led_name, sizeof(blinkm_led_name),
617 leds[i]->cdev.led_cdev.name = blinkm_led_name;
618 leds[i]->cdev.led_cdev.brightness_set_blocking =
620 err = led_classdev_register(&client->dev,
621 &leds[i]->cdev.led_cdev);
623 dev_err(&client->dev,
624 "couldn't register LED %s\n",
625 leds[i]->cdev.led_cdev.name);
630 scnprintf(blinkm_led_name, sizeof(blinkm_led_name),
631 "blinkm-%d-%d-green",
634 leds[i]->cdev.led_cdev.name = blinkm_led_name;
635 leds[i]->cdev.led_cdev.brightness_set_blocking =
636 blinkm_led_green_set;
637 err = led_classdev_register(&client->dev,
638 &leds[i]->cdev.led_cdev);
640 dev_err(&client->dev,
641 "couldn't register LED %s\n",
642 leds[i]->cdev.led_cdev.name);
647 scnprintf(blinkm_led_name, sizeof(blinkm_led_name),
651 leds[i]->cdev.led_cdev.name = blinkm_led_name;
652 leds[i]->cdev.led_cdev.brightness_set_blocking =
654 err = led_classdev_register(&client->dev,
655 &leds[i]->cdev.led_cdev);
657 dev_err(&client->dev,
658 "couldn't register LED %s\n",
659 leds[i]->cdev.led_cdev.name);
670 led_classdev_unregister(&leds[GREEN]->cdev.led_cdev);
672 led_classdev_unregister(&leds[RED]->cdev.led_cdev);
674 sysfs_remove_group(&client->dev.kobj, &blinkm_group);
679 static int register_multicolor(struct i2c_client *client, struct blinkm_data *data)
681 struct blinkm_led *mc_led;
682 struct mc_subled *mc_led_info;
683 char blinkm_led_name[28];
686 /* Register multicolor sysfs class */
687 /* The first element of leds is used for multicolor facilities */
688 mc_led = &data->blinkm_leds[RED];
689 mc_led->i2c_client = client;
691 mc_led_info = devm_kcalloc(&client->dev, NUM_LEDS, sizeof(*mc_led_info),
696 mc_led_info[RED].color_index = LED_COLOR_ID_RED;
697 mc_led_info[GREEN].color_index = LED_COLOR_ID_GREEN;
698 mc_led_info[BLUE].color_index = LED_COLOR_ID_BLUE;
700 mc_led->cdev.mcled_cdev.subled_info = mc_led_info;
701 mc_led->cdev.mcled_cdev.num_colors = NUM_LEDS;
702 mc_led->cdev.mcled_cdev.led_cdev.brightness = 255;
703 mc_led->cdev.mcled_cdev.led_cdev.max_brightness = 255;
704 mc_led->cdev.mcled_cdev.led_cdev.flags = LED_CORE_SUSPENDRESUME;
706 scnprintf(blinkm_led_name, sizeof(blinkm_led_name),
707 "blinkm-%d-%d:rgb:indicator",
710 mc_led->cdev.mcled_cdev.led_cdev.name = blinkm_led_name;
711 mc_led->cdev.mcled_cdev.led_cdev.brightness_set_blocking = blinkm_set_mc_brightness;
713 err = led_classdev_multicolor_register(&client->dev, &mc_led->cdev.mcled_cdev);
715 dev_err(&client->dev, "couldn't register LED %s\n",
716 mc_led->cdev.led_cdev.name);
717 sysfs_remove_group(&client->dev.kobj, &blinkm_group);
722 static int blinkm_probe(struct i2c_client *client)
724 struct blinkm_data *data;
727 data = devm_kzalloc(&client->dev,
728 sizeof(struct blinkm_data), GFP_KERNEL);
732 data->i2c_addr = 0x08;
733 /* i2c addr - use fake addr of 0x08 initially (real is 0x09) */
735 /* firmware version - use fake until we read real value
736 * (currently broken - BlinkM confused!)
738 data->script_id = 0x01;
739 data->i2c_client = client;
741 i2c_set_clientdata(client, data);
742 mutex_init(&data->update_lock);
744 /* Register sysfs hooks */
745 err = sysfs_create_group(&client->dev.kobj, &blinkm_group);
747 dev_err(&client->dev, "couldn't register sysfs group\n");
751 if (!IS_ENABLED(CONFIG_LEDS_BLINKM_MULTICOLOR)) {
752 err = register_separate_colors(client, data);
756 err = register_multicolor(client, data);
761 blinkm_init_hw(client);
766 static void blinkm_remove(struct i2c_client *client)
768 struct blinkm_data *data = i2c_get_clientdata(client);
772 /* make sure no workqueue entries are pending */
773 for (i = 0; i < NUM_LEDS; i++)
774 led_classdev_unregister(&data->blinkm_leds[i].cdev.led_cdev);
777 data->next_red = 0x00;
778 data->next_green = 0x00;
779 data->next_blue = 0x00;
780 ret = blinkm_transfer_hw(client, BLM_FADE_RGB);
782 dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
785 data->next_hue = 0x00;
786 data->next_saturation = 0x00;
787 data->next_brightness = 0x00;
788 ret = blinkm_transfer_hw(client, BLM_FADE_HSB);
790 dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
792 /* red fade to off */
793 data->next_red = 0xff;
794 ret = blinkm_transfer_hw(client, BLM_GO_RGB);
796 dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
799 data->next_red = 0x00;
800 ret = blinkm_transfer_hw(client, BLM_FADE_RGB);
802 dev_err(&client->dev, "Failure in blinkm_remove ignored. Continuing.\n");
804 sysfs_remove_group(&client->dev.kobj, &blinkm_group);
807 static const struct i2c_device_id blinkm_id[] = {
812 MODULE_DEVICE_TABLE(i2c, blinkm_id);
814 /* This is the driver that will be inserted */
815 static struct i2c_driver blinkm_driver = {
816 .class = I2C_CLASS_HWMON,
820 .probe = blinkm_probe,
821 .remove = blinkm_remove,
822 .id_table = blinkm_id,
823 .detect = blinkm_detect,
824 .address_list = normal_i2c,
827 module_i2c_driver(blinkm_driver);
831 MODULE_DESCRIPTION("BlinkM RGB LED driver");
832 MODULE_LICENSE("GPL");