3 * (c) Jan-Simon Möller (dl9pf@gmx.de)
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.
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.
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.
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/jiffies.h>
23 #include <linux/i2c.h>
24 #include <linux/err.h>
25 #include <linux/mutex.h>
26 #include <linux/sysfs.h>
27 #include <linux/printk.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/leds.h>
30 #include <linux/delay.h>
32 /* Addresses to scan - BlinkM is on 0x09 by default*/
33 static const unsigned short normal_i2c
[] = { 0x09, I2C_CLIENT_END
};
35 static int blinkm_transfer_hw(struct i2c_client
*client
, int cmd
);
36 static int blinkm_test_run(struct i2c_client
*client
);
39 struct i2c_client
*i2c_client
;
40 struct led_classdev led_cdev
;
46 struct blinkm_led
*blinkm_led
;
47 struct work_struct work
;
50 #define cdev_to_blmled(c) container_of(c, struct blinkm_led, led_cdev)
51 #define work_to_blmwork(c) container_of(c, struct blinkm_work, work)
54 struct i2c_client
*i2c_client
;
55 struct mutex update_lock
;
56 /* used for led class interface */
57 struct blinkm_led blinkm_leds
[3];
58 /* used for "blinkm" sysfs interface */
59 u8 red
; /* color red */
60 u8 green
; /* color green */
61 u8 blue
; /* color blue */
62 /* next values to use for transfer */
63 u8 next_red
; /* color red */
64 u8 next_green
; /* color green */
65 u8 next_blue
; /* color blue */
67 u8 args
[7]; /* set of args for transmission */
68 u8 i2c_addr
; /* i2c addr */
69 u8 fw_ver
; /* firmware version */
70 /* used, but not from userspace */
72 u8 saturation
; /* HSB saturation */
73 u8 brightness
; /* HSB brightness */
74 u8 next_hue
; /* HSB hue */
75 u8 next_saturation
; /* HSB saturation */
76 u8 next_brightness
; /* HSB brightness */
77 /* currently unused / todo */
78 u8 fade_speed
; /* fade speed 1 - 255 */
79 s8 time_adjust
; /* time adjust -128 - 127 */
80 u8 fade
:1; /* fade on = 1, off = 0 */
81 u8 rand
:1; /* rand fade mode on = 1 */
82 u8 script_id
; /* script ID */
83 u8 script_repeats
; /* repeats of script */
84 u8 script_startline
; /* line to start */
92 /* mapping command names to cmd chars - see datasheet */
94 #define BLM_FADE_RGB 1
95 #define BLM_FADE_HSB 2
96 #define BLM_FADE_RAND_RGB 3
97 #define BLM_FADE_RAND_HSB 4
98 #define BLM_PLAY_SCRIPT 5
99 #define BLM_STOP_SCRIPT 6
100 #define BLM_SET_FADE_SPEED 7
101 #define BLM_SET_TIME_ADJ 8
102 #define BLM_GET_CUR_RGB 9
103 #define BLM_WRITE_SCRIPT_LINE 10
104 #define BLM_READ_SCRIPT_LINE 11
105 #define BLM_SET_SCRIPT_LR 12 /* Length & Repeats */
106 #define BLM_SET_ADDR 13
107 #define BLM_GET_ADDR 14
108 #define BLM_GET_FW_VER 15
109 #define BLM_SET_STARTUP_PARAM 16
112 * as extracted out of the datasheet:
114 * cmdchar = command (ascii)
115 * cmdbyte = command in hex
116 * nr_args = number of arguments (to send)
117 * nr_ret = number of return values (to read)
118 * dir = direction (0 = read, 1 = write, 2 = both)
121 static const struct {
127 } blinkm_cmds
[17] = {
128 /* cmdchar, cmdbyte, nr_args, nr_ret, dir */
129 { 'n', 0x6e, 3, 0, 1},
130 { 'c', 0x63, 3, 0, 1},
131 { 'h', 0x68, 3, 0, 1},
132 { 'C', 0x43, 3, 0, 1},
133 { 'H', 0x48, 3, 0, 1},
134 { 'p', 0x70, 3, 0, 1},
135 { 'o', 0x6f, 0, 0, 1},
136 { 'f', 0x66, 1, 0, 1},
137 { 't', 0x74, 1, 0, 1},
138 { 'g', 0x67, 0, 3, 0},
139 { 'W', 0x57, 7, 0, 1},
140 { 'R', 0x52, 2, 5, 2},
141 { 'L', 0x4c, 3, 0, 1},
142 { 'A', 0x41, 4, 0, 1},
143 { 'a', 0x61, 0, 1, 0},
144 { 'Z', 0x5a, 0, 1, 0},
145 { 'B', 0x42, 5, 0, 1},
148 static ssize_t
show_color_common(struct device
*dev
, char *buf
, int color
)
150 struct i2c_client
*client
;
151 struct blinkm_data
*data
;
154 client
= to_i2c_client(dev
);
155 data
= i2c_get_clientdata(client
);
157 ret
= blinkm_transfer_hw(client
, BLM_GET_CUR_RGB
);
162 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->red
);
164 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->green
);
166 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->blue
);
173 static int store_color_common(struct device
*dev
, const char *buf
, int color
)
175 struct i2c_client
*client
;
176 struct blinkm_data
*data
;
180 client
= to_i2c_client(dev
);
181 data
= i2c_get_clientdata(client
);
183 ret
= kstrtou8(buf
, 10, &value
);
185 dev_err(dev
, "BlinkM: value too large!\n");
191 data
->next_red
= value
;
194 data
->next_green
= value
;
197 data
->next_blue
= value
;
203 dev_dbg(dev
, "next_red = %d, next_green = %d, next_blue = %d\n",
204 data
->next_red
, data
->next_green
, data
->next_blue
);
207 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
209 dev_err(dev
, "BlinkM: can't set RGB\n");
215 static ssize_t
show_red(struct device
*dev
, struct device_attribute
*attr
,
218 return show_color_common(dev
, buf
, RED
);
221 static ssize_t
store_red(struct device
*dev
, struct device_attribute
*attr
,
222 const char *buf
, size_t count
)
226 ret
= store_color_common(dev
, buf
, RED
);
232 static DEVICE_ATTR(red
, S_IRUGO
| S_IWUSR
, show_red
, store_red
);
234 static ssize_t
show_green(struct device
*dev
, struct device_attribute
*attr
,
237 return show_color_common(dev
, buf
, GREEN
);
240 static ssize_t
store_green(struct device
*dev
, struct device_attribute
*attr
,
241 const char *buf
, size_t count
)
246 ret
= store_color_common(dev
, buf
, GREEN
);
252 static DEVICE_ATTR(green
, S_IRUGO
| S_IWUSR
, show_green
, store_green
);
254 static ssize_t
show_blue(struct device
*dev
, struct device_attribute
*attr
,
257 return show_color_common(dev
, buf
, BLUE
);
260 static ssize_t
store_blue(struct device
*dev
, struct device_attribute
*attr
,
261 const char *buf
, size_t count
)
265 ret
= store_color_common(dev
, buf
, BLUE
);
271 static DEVICE_ATTR(blue
, S_IRUGO
| S_IWUSR
, show_blue
, store_blue
);
273 static ssize_t
show_test(struct device
*dev
, struct device_attribute
*attr
,
276 return scnprintf(buf
, PAGE_SIZE
,
277 "#Write into test to start test sequence!#\n");
280 static ssize_t
store_test(struct device
*dev
, struct device_attribute
*attr
,
281 const char *buf
, size_t count
)
284 struct i2c_client
*client
;
286 client
= to_i2c_client(dev
);
289 ret
= blinkm_test_run(client
);
296 static DEVICE_ATTR(test
, S_IRUGO
| S_IWUSR
, show_test
, store_test
);
298 /* TODO: HSB, fade, timeadj, script ... */
300 static struct attribute
*blinkm_attrs
[] = {
302 &dev_attr_green
.attr
,
308 static struct attribute_group blinkm_group
= {
310 .attrs
= blinkm_attrs
,
313 static int blinkm_write(struct i2c_client
*client
, int cmd
, u8
*arg
)
317 int arglen
= blinkm_cmds
[cmd
].nr_args
;
318 /* write out cmd to blinkm - always / default step */
319 result
= i2c_smbus_write_byte(client
, blinkm_cmds
[cmd
].cmdbyte
);
322 /* no args to write out */
326 for (i
= 0; i
< arglen
; i
++) {
327 /* repeat for arglen */
328 result
= i2c_smbus_write_byte(client
, arg
[i
]);
335 static int blinkm_read(struct i2c_client
*client
, int cmd
, u8
*arg
)
339 int retlen
= blinkm_cmds
[cmd
].nr_ret
;
340 for (i
= 0; i
< retlen
; i
++) {
341 /* repeat for retlen */
342 result
= i2c_smbus_read_byte(client
);
351 static int blinkm_transfer_hw(struct i2c_client
*client
, int cmd
)
353 /* the protocol is simple but non-standard:
354 * e.g. cmd 'g' (= 0x67) for "get device address"
355 * - which defaults to 0x09 - would be the sequence:
356 * a) write 0x67 to the device (byte write)
357 * b) read the value (0x09) back right after (byte read)
359 * Watch out for "unfinished" sequences (i.e. not enough reads
360 * or writes after a command. It will make the blinkM misbehave.
361 * Sequence is key here.
364 /* args / return are in private data struct */
365 struct blinkm_data
*data
= i2c_get_clientdata(client
);
367 /* We start hardware transfers which are not to be
368 * mixed with other commands. Aquire a lock now. */
369 if (mutex_lock_interruptible(&data
->update_lock
) < 0)
372 /* switch cmd - usually write before reads */
374 case BLM_FADE_RAND_RGB
:
377 data
->args
[0] = data
->next_red
;
378 data
->args
[1] = data
->next_green
;
379 data
->args
[2] = data
->next_blue
;
380 blinkm_write(client
, cmd
, data
->args
);
381 data
->red
= data
->args
[0];
382 data
->green
= data
->args
[1];
383 data
->blue
= data
->args
[2];
386 case BLM_FADE_RAND_HSB
:
387 data
->args
[0] = data
->next_hue
;
388 data
->args
[1] = data
->next_saturation
;
389 data
->args
[2] = data
->next_brightness
;
390 blinkm_write(client
, cmd
, data
->args
);
391 data
->hue
= data
->next_hue
;
392 data
->saturation
= data
->next_saturation
;
393 data
->brightness
= data
->next_brightness
;
395 case BLM_PLAY_SCRIPT
:
396 data
->args
[0] = data
->script_id
;
397 data
->args
[1] = data
->script_repeats
;
398 data
->args
[2] = data
->script_startline
;
399 blinkm_write(client
, cmd
, data
->args
);
401 case BLM_STOP_SCRIPT
:
402 blinkm_write(client
, cmd
, NULL
);
404 case BLM_GET_CUR_RGB
:
405 data
->args
[0] = data
->red
;
406 data
->args
[1] = data
->green
;
407 data
->args
[2] = data
->blue
;
408 blinkm_write(client
, cmd
, NULL
);
409 blinkm_read(client
, cmd
, data
->args
);
410 data
->red
= data
->args
[0];
411 data
->green
= data
->args
[1];
412 data
->blue
= data
->args
[2];
415 data
->args
[0] = data
->i2c_addr
;
416 blinkm_write(client
, cmd
, NULL
);
417 blinkm_read(client
, cmd
, data
->args
);
418 data
->i2c_addr
= data
->args
[0];
420 case BLM_SET_TIME_ADJ
:
421 case BLM_SET_FADE_SPEED
:
422 case BLM_READ_SCRIPT_LINE
:
423 case BLM_WRITE_SCRIPT_LINE
:
424 case BLM_SET_SCRIPT_LR
:
427 case BLM_SET_STARTUP_PARAM
:
428 dev_err(&client
->dev
,
429 "BlinkM: cmd %d not implemented yet.\n", cmd
);
432 dev_err(&client
->dev
, "BlinkM: unknown command %d\n", cmd
);
433 mutex_unlock(&data
->update_lock
);
435 } /* end switch(cmd) */
437 /* transfers done, unlock */
438 mutex_unlock(&data
->update_lock
);
442 static void led_work(struct work_struct
*work
)
445 struct blinkm_led
*led
;
446 struct blinkm_data
*data
;
447 struct blinkm_work
*blm_work
= work_to_blmwork(work
);
449 led
= blm_work
->blinkm_led
;
450 data
= i2c_get_clientdata(led
->i2c_client
);
451 ret
= blinkm_transfer_hw(led
->i2c_client
, BLM_GO_RGB
);
452 atomic_dec(&led
->active
);
453 dev_dbg(&led
->i2c_client
->dev
,
454 "# DONE # next_red = %d, next_green = %d,"
455 " next_blue = %d, active = %d\n",
456 data
->next_red
, data
->next_green
,
457 data
->next_blue
, atomic_read(&led
->active
));
461 static int blinkm_led_common_set(struct led_classdev
*led_cdev
,
462 enum led_brightness value
, int color
)
464 /* led_brightness is 0, 127 or 255 - we just use it here as-is */
465 struct blinkm_led
*led
= cdev_to_blmled(led_cdev
);
466 struct blinkm_data
*data
= i2c_get_clientdata(led
->i2c_client
);
467 struct blinkm_work
*bl_work
;
471 /* bail out if there's no change */
472 if (data
->next_red
== (u8
) value
)
474 /* we assume a quite fast sequence here ([off]->on->off)
475 * think of network led trigger - we cannot blink that fast, so
476 * in case we already have a off->on->off transition queued up,
477 * we refuse to queue up more.
478 * Revisit: fast-changing brightness. */
479 if (atomic_read(&led
->active
) > 1)
481 data
->next_red
= (u8
) value
;
484 /* bail out if there's no change */
485 if (data
->next_green
== (u8
) value
)
487 /* we assume a quite fast sequence here ([off]->on->off)
488 * Revisit: fast-changing brightness. */
489 if (atomic_read(&led
->active
) > 1)
491 data
->next_green
= (u8
) value
;
494 /* bail out if there's no change */
495 if (data
->next_blue
== (u8
) value
)
497 /* we assume a quite fast sequence here ([off]->on->off)
498 * Revisit: fast-changing brightness. */
499 if (atomic_read(&led
->active
) > 1)
501 data
->next_blue
= (u8
) value
;
505 dev_err(&led
->i2c_client
->dev
, "BlinkM: unknown color.\n");
509 bl_work
= kzalloc(sizeof(*bl_work
), GFP_ATOMIC
);
513 atomic_inc(&led
->active
);
514 dev_dbg(&led
->i2c_client
->dev
,
515 "#TO_SCHED# next_red = %d, next_green = %d,"
516 " next_blue = %d, active = %d\n",
517 data
->next_red
, data
->next_green
,
518 data
->next_blue
, atomic_read(&led
->active
));
520 /* a fresh work _item_ for each change */
521 bl_work
->blinkm_led
= led
;
522 INIT_WORK(&bl_work
->work
, led_work
);
523 /* queue work in own queue for easy sync on exit*/
524 schedule_work(&bl_work
->work
);
529 static void blinkm_led_red_set(struct led_classdev
*led_cdev
,
530 enum led_brightness value
)
532 blinkm_led_common_set(led_cdev
, value
, RED
);
535 static void blinkm_led_green_set(struct led_classdev
*led_cdev
,
536 enum led_brightness value
)
538 blinkm_led_common_set(led_cdev
, value
, GREEN
);
541 static void blinkm_led_blue_set(struct led_classdev
*led_cdev
,
542 enum led_brightness value
)
544 blinkm_led_common_set(led_cdev
, value
, BLUE
);
547 static void blinkm_init_hw(struct i2c_client
*client
)
550 ret
= blinkm_transfer_hw(client
, BLM_STOP_SCRIPT
);
551 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
554 static int blinkm_test_run(struct i2c_client
*client
)
557 struct blinkm_data
*data
= i2c_get_clientdata(client
);
559 data
->next_red
= 0x01;
560 data
->next_green
= 0x05;
561 data
->next_blue
= 0x10;
562 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
567 data
->next_red
= 0x25;
568 data
->next_green
= 0x10;
569 data
->next_blue
= 0x31;
570 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
575 data
->next_hue
= 0x50;
576 data
->next_saturation
= 0x10;
577 data
->next_brightness
= 0x20;
578 ret
= blinkm_transfer_hw(client
, BLM_FADE_HSB
);
586 /* Return 0 if detection is successful, -ENODEV otherwise */
587 static int blinkm_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
589 struct i2c_adapter
*adapter
= client
->adapter
;
594 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
595 | I2C_FUNC_SMBUS_WORD_DATA
596 | I2C_FUNC_SMBUS_WRITE_BYTE
))
599 /* Now, we do the remaining detection. Simple for now. */
600 /* We might need more guards to protect other i2c slaves */
602 /* make sure the blinkM is balanced (read/writes) */
604 ret
= blinkm_write(client
, BLM_GET_ADDR
, NULL
);
605 usleep_range(5000, 10000);
606 ret
= blinkm_read(client
, BLM_GET_ADDR
, tmpargs
);
607 usleep_range(5000, 10000);
608 if (tmpargs
[0] == 0x09)
613 /* Step 1: Read BlinkM address back - cmd_char 'a' */
614 ret
= blinkm_write(client
, BLM_GET_ADDR
, NULL
);
617 usleep_range(20000, 30000); /* allow a small delay */
618 ret
= blinkm_read(client
, BLM_GET_ADDR
, tmpargs
);
622 if (tmpargs
[0] != 0x09) {
623 dev_err(&client
->dev
, "enodev DEV ADDR = 0x%02X\n", tmpargs
[0]);
627 strlcpy(info
->type
, "blinkm", I2C_NAME_SIZE
);
631 static int blinkm_probe(struct i2c_client
*client
,
632 const struct i2c_device_id
*id
)
634 struct blinkm_data
*data
;
635 struct blinkm_led
*led
[3];
637 char blinkm_led_name
[28];
639 data
= devm_kzalloc(&client
->dev
,
640 sizeof(struct blinkm_data
), GFP_KERNEL
);
646 data
->i2c_addr
= 0x09;
647 data
->i2c_addr
= 0x08;
648 /* i2c addr - use fake addr of 0x08 initially (real is 0x09) */
650 /* firmware version - use fake until we read real value
651 * (currently broken - BlinkM confused!) */
652 data
->script_id
= 0x01;
653 data
->i2c_client
= client
;
655 i2c_set_clientdata(client
, data
);
656 mutex_init(&data
->update_lock
);
658 /* Register sysfs hooks */
659 err
= sysfs_create_group(&client
->dev
.kobj
, &blinkm_group
);
661 dev_err(&client
->dev
, "couldn't register sysfs group\n");
665 for (i
= 0; i
< 3; i
++) {
666 /* RED = 0, GREEN = 1, BLUE = 2 */
667 led
[i
] = &data
->blinkm_leds
[i
];
668 led
[i
]->i2c_client
= client
;
670 led
[i
]->led_cdev
.max_brightness
= 255;
671 led
[i
]->led_cdev
.flags
= LED_CORE_SUSPENDRESUME
;
672 atomic_set(&led
[i
]->active
, 0);
675 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
679 led
[i
]->led_cdev
.name
= blinkm_led_name
;
680 led
[i
]->led_cdev
.brightness_set
= blinkm_led_red_set
;
681 err
= led_classdev_register(&client
->dev
,
684 dev_err(&client
->dev
,
685 "couldn't register LED %s\n",
686 led
[i
]->led_cdev
.name
);
691 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
692 "blinkm-%d-%d-green",
695 led
[i
]->led_cdev
.name
= blinkm_led_name
;
696 led
[i
]->led_cdev
.brightness_set
= blinkm_led_green_set
;
697 err
= led_classdev_register(&client
->dev
,
700 dev_err(&client
->dev
,
701 "couldn't register LED %s\n",
702 led
[i
]->led_cdev
.name
);
707 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
711 led
[i
]->led_cdev
.name
= blinkm_led_name
;
712 led
[i
]->led_cdev
.brightness_set
= blinkm_led_blue_set
;
713 err
= led_classdev_register(&client
->dev
,
716 dev_err(&client
->dev
,
717 "couldn't register LED %s\n",
718 led
[i
]->led_cdev
.name
);
725 /* Initialize the blinkm */
726 blinkm_init_hw(client
);
731 led_classdev_unregister(&led
[GREEN
]->led_cdev
);
734 led_classdev_unregister(&led
[RED
]->led_cdev
);
737 sysfs_remove_group(&client
->dev
.kobj
, &blinkm_group
);
742 static int blinkm_remove(struct i2c_client
*client
)
744 struct blinkm_data
*data
= i2c_get_clientdata(client
);
748 /* make sure no workqueue entries are pending */
749 for (i
= 0; i
< 3; i
++) {
750 flush_scheduled_work();
751 led_classdev_unregister(&data
->blinkm_leds
[i
].led_cdev
);
755 data
->next_red
= 0x00;
756 data
->next_green
= 0x00;
757 data
->next_blue
= 0x00;
758 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
760 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
763 data
->next_hue
= 0x00;
764 data
->next_saturation
= 0x00;
765 data
->next_brightness
= 0x00;
766 ret
= blinkm_transfer_hw(client
, BLM_FADE_HSB
);
768 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
770 /* red fade to off */
771 data
->next_red
= 0xff;
772 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
774 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
777 data
->next_red
= 0x00;
778 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
780 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
782 sysfs_remove_group(&client
->dev
.kobj
, &blinkm_group
);
786 static const struct i2c_device_id blinkm_id
[] = {
791 MODULE_DEVICE_TABLE(i2c
, blinkm_id
);
793 /* This is the driver that will be inserted */
794 static struct i2c_driver blinkm_driver
= {
795 .class = I2C_CLASS_HWMON
,
799 .probe
= blinkm_probe
,
800 .remove
= blinkm_remove
,
801 .id_table
= blinkm_id
,
802 .detect
= blinkm_detect
,
803 .address_list
= normal_i2c
,
806 module_i2c_driver(blinkm_driver
);
808 MODULE_AUTHOR("Jan-Simon Moeller <dl9pf@gmx.de>");
809 MODULE_DESCRIPTION("BlinkM RGB LED driver");
810 MODULE_LICENSE("GPL");