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
;
44 #define cdev_to_blmled(c) container_of(c, struct blinkm_led, led_cdev)
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 */
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 */
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 */
85 /* mapping command names to cmd chars - see datasheet */
87 #define BLM_FADE_RGB 1
88 #define BLM_FADE_HSB 2
89 #define BLM_FADE_RAND_RGB 3
90 #define BLM_FADE_RAND_HSB 4
91 #define BLM_PLAY_SCRIPT 5
92 #define BLM_STOP_SCRIPT 6
93 #define BLM_SET_FADE_SPEED 7
94 #define BLM_SET_TIME_ADJ 8
95 #define BLM_GET_CUR_RGB 9
96 #define BLM_WRITE_SCRIPT_LINE 10
97 #define BLM_READ_SCRIPT_LINE 11
98 #define BLM_SET_SCRIPT_LR 12 /* Length & Repeats */
99 #define BLM_SET_ADDR 13
100 #define BLM_GET_ADDR 14
101 #define BLM_GET_FW_VER 15
102 #define BLM_SET_STARTUP_PARAM 16
105 * as extracted out of the datasheet:
107 * cmdchar = command (ascii)
108 * cmdbyte = command in hex
109 * nr_args = number of arguments (to send)
110 * nr_ret = number of return values (to read)
111 * dir = direction (0 = read, 1 = write, 2 = both)
114 static const struct {
120 } blinkm_cmds
[17] = {
121 /* cmdchar, cmdbyte, nr_args, nr_ret, dir */
122 { 'n', 0x6e, 3, 0, 1},
123 { 'c', 0x63, 3, 0, 1},
124 { 'h', 0x68, 3, 0, 1},
125 { 'C', 0x43, 3, 0, 1},
126 { 'H', 0x48, 3, 0, 1},
127 { 'p', 0x70, 3, 0, 1},
128 { 'o', 0x6f, 0, 0, 1},
129 { 'f', 0x66, 1, 0, 1},
130 { 't', 0x74, 1, 0, 1},
131 { 'g', 0x67, 0, 3, 0},
132 { 'W', 0x57, 7, 0, 1},
133 { 'R', 0x52, 2, 5, 2},
134 { 'L', 0x4c, 3, 0, 1},
135 { 'A', 0x41, 4, 0, 1},
136 { 'a', 0x61, 0, 1, 0},
137 { 'Z', 0x5a, 0, 1, 0},
138 { 'B', 0x42, 5, 0, 1},
141 static ssize_t
show_color_common(struct device
*dev
, char *buf
, int color
)
143 struct i2c_client
*client
;
144 struct blinkm_data
*data
;
147 client
= to_i2c_client(dev
);
148 data
= i2c_get_clientdata(client
);
150 ret
= blinkm_transfer_hw(client
, BLM_GET_CUR_RGB
);
155 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->red
);
157 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->green
);
159 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->blue
);
166 static int store_color_common(struct device
*dev
, const char *buf
, int color
)
168 struct i2c_client
*client
;
169 struct blinkm_data
*data
;
173 client
= to_i2c_client(dev
);
174 data
= i2c_get_clientdata(client
);
176 ret
= kstrtou8(buf
, 10, &value
);
178 dev_err(dev
, "BlinkM: value too large!\n");
184 data
->next_red
= value
;
187 data
->next_green
= value
;
190 data
->next_blue
= value
;
196 dev_dbg(dev
, "next_red = %d, next_green = %d, next_blue = %d\n",
197 data
->next_red
, data
->next_green
, data
->next_blue
);
200 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
202 dev_err(dev
, "BlinkM: can't set RGB\n");
208 static ssize_t
show_red(struct device
*dev
, struct device_attribute
*attr
,
211 return show_color_common(dev
, buf
, RED
);
214 static ssize_t
store_red(struct device
*dev
, struct device_attribute
*attr
,
215 const char *buf
, size_t count
)
219 ret
= store_color_common(dev
, buf
, RED
);
225 static DEVICE_ATTR(red
, S_IRUGO
| S_IWUSR
, show_red
, store_red
);
227 static ssize_t
show_green(struct device
*dev
, struct device_attribute
*attr
,
230 return show_color_common(dev
, buf
, GREEN
);
233 static ssize_t
store_green(struct device
*dev
, struct device_attribute
*attr
,
234 const char *buf
, size_t count
)
239 ret
= store_color_common(dev
, buf
, GREEN
);
245 static DEVICE_ATTR(green
, S_IRUGO
| S_IWUSR
, show_green
, store_green
);
247 static ssize_t
show_blue(struct device
*dev
, struct device_attribute
*attr
,
250 return show_color_common(dev
, buf
, BLUE
);
253 static ssize_t
store_blue(struct device
*dev
, struct device_attribute
*attr
,
254 const char *buf
, size_t count
)
258 ret
= store_color_common(dev
, buf
, BLUE
);
264 static DEVICE_ATTR(blue
, S_IRUGO
| S_IWUSR
, show_blue
, store_blue
);
266 static ssize_t
show_test(struct device
*dev
, struct device_attribute
*attr
,
269 return scnprintf(buf
, PAGE_SIZE
,
270 "#Write into test to start test sequence!#\n");
273 static ssize_t
store_test(struct device
*dev
, struct device_attribute
*attr
,
274 const char *buf
, size_t count
)
277 struct i2c_client
*client
;
279 client
= to_i2c_client(dev
);
282 ret
= blinkm_test_run(client
);
289 static DEVICE_ATTR(test
, S_IRUGO
| S_IWUSR
, show_test
, store_test
);
291 /* TODO: HSB, fade, timeadj, script ... */
293 static struct attribute
*blinkm_attrs
[] = {
295 &dev_attr_green
.attr
,
301 static struct attribute_group blinkm_group
= {
303 .attrs
= blinkm_attrs
,
306 static int blinkm_write(struct i2c_client
*client
, int cmd
, u8
*arg
)
310 int arglen
= blinkm_cmds
[cmd
].nr_args
;
311 /* write out cmd to blinkm - always / default step */
312 result
= i2c_smbus_write_byte(client
, blinkm_cmds
[cmd
].cmdbyte
);
315 /* no args to write out */
319 for (i
= 0; i
< arglen
; i
++) {
320 /* repeat for arglen */
321 result
= i2c_smbus_write_byte(client
, arg
[i
]);
328 static int blinkm_read(struct i2c_client
*client
, int cmd
, u8
*arg
)
332 int retlen
= blinkm_cmds
[cmd
].nr_ret
;
333 for (i
= 0; i
< retlen
; i
++) {
334 /* repeat for retlen */
335 result
= i2c_smbus_read_byte(client
);
344 static int blinkm_transfer_hw(struct i2c_client
*client
, int cmd
)
346 /* the protocol is simple but non-standard:
347 * e.g. cmd 'g' (= 0x67) for "get device address"
348 * - which defaults to 0x09 - would be the sequence:
349 * a) write 0x67 to the device (byte write)
350 * b) read the value (0x09) back right after (byte read)
352 * Watch out for "unfinished" sequences (i.e. not enough reads
353 * or writes after a command. It will make the blinkM misbehave.
354 * Sequence is key here.
357 /* args / return are in private data struct */
358 struct blinkm_data
*data
= i2c_get_clientdata(client
);
360 /* We start hardware transfers which are not to be
361 * mixed with other commands. Aquire a lock now. */
362 if (mutex_lock_interruptible(&data
->update_lock
) < 0)
365 /* switch cmd - usually write before reads */
367 case BLM_FADE_RAND_RGB
:
370 data
->args
[0] = data
->next_red
;
371 data
->args
[1] = data
->next_green
;
372 data
->args
[2] = data
->next_blue
;
373 blinkm_write(client
, cmd
, data
->args
);
374 data
->red
= data
->args
[0];
375 data
->green
= data
->args
[1];
376 data
->blue
= data
->args
[2];
379 case BLM_FADE_RAND_HSB
:
380 data
->args
[0] = data
->next_hue
;
381 data
->args
[1] = data
->next_saturation
;
382 data
->args
[2] = data
->next_brightness
;
383 blinkm_write(client
, cmd
, data
->args
);
384 data
->hue
= data
->next_hue
;
385 data
->saturation
= data
->next_saturation
;
386 data
->brightness
= data
->next_brightness
;
388 case BLM_PLAY_SCRIPT
:
389 data
->args
[0] = data
->script_id
;
390 data
->args
[1] = data
->script_repeats
;
391 data
->args
[2] = data
->script_startline
;
392 blinkm_write(client
, cmd
, data
->args
);
394 case BLM_STOP_SCRIPT
:
395 blinkm_write(client
, cmd
, NULL
);
397 case BLM_GET_CUR_RGB
:
398 data
->args
[0] = data
->red
;
399 data
->args
[1] = data
->green
;
400 data
->args
[2] = data
->blue
;
401 blinkm_write(client
, cmd
, NULL
);
402 blinkm_read(client
, cmd
, data
->args
);
403 data
->red
= data
->args
[0];
404 data
->green
= data
->args
[1];
405 data
->blue
= data
->args
[2];
408 data
->args
[0] = data
->i2c_addr
;
409 blinkm_write(client
, cmd
, NULL
);
410 blinkm_read(client
, cmd
, data
->args
);
411 data
->i2c_addr
= data
->args
[0];
413 case BLM_SET_TIME_ADJ
:
414 case BLM_SET_FADE_SPEED
:
415 case BLM_READ_SCRIPT_LINE
:
416 case BLM_WRITE_SCRIPT_LINE
:
417 case BLM_SET_SCRIPT_LR
:
420 case BLM_SET_STARTUP_PARAM
:
421 dev_err(&client
->dev
,
422 "BlinkM: cmd %d not implemented yet.\n", cmd
);
425 dev_err(&client
->dev
, "BlinkM: unknown command %d\n", cmd
);
426 mutex_unlock(&data
->update_lock
);
428 } /* end switch(cmd) */
430 /* transfers done, unlock */
431 mutex_unlock(&data
->update_lock
);
435 static int blinkm_led_common_set(struct led_classdev
*led_cdev
,
436 enum led_brightness value
, int color
)
438 /* led_brightness is 0, 127 or 255 - we just use it here as-is */
439 struct blinkm_led
*led
= cdev_to_blmled(led_cdev
);
440 struct blinkm_data
*data
= i2c_get_clientdata(led
->i2c_client
);
444 /* bail out if there's no change */
445 if (data
->next_red
== (u8
) value
)
447 data
->next_red
= (u8
) value
;
450 /* bail out if there's no change */
451 if (data
->next_green
== (u8
) value
)
453 data
->next_green
= (u8
) value
;
456 /* bail out if there's no change */
457 if (data
->next_blue
== (u8
) value
)
459 data
->next_blue
= (u8
) value
;
463 dev_err(&led
->i2c_client
->dev
, "BlinkM: unknown color.\n");
467 blinkm_transfer_hw(led
->i2c_client
, BLM_GO_RGB
);
468 dev_dbg(&led
->i2c_client
->dev
,
469 "# DONE # next_red = %d, next_green = %d,"
471 data
->next_red
, data
->next_green
,
476 static int blinkm_led_red_set(struct led_classdev
*led_cdev
,
477 enum led_brightness value
)
479 return blinkm_led_common_set(led_cdev
, value
, RED
);
482 static int blinkm_led_green_set(struct led_classdev
*led_cdev
,
483 enum led_brightness value
)
485 return blinkm_led_common_set(led_cdev
, value
, GREEN
);
488 static int blinkm_led_blue_set(struct led_classdev
*led_cdev
,
489 enum led_brightness value
)
491 return blinkm_led_common_set(led_cdev
, value
, BLUE
);
494 static void blinkm_init_hw(struct i2c_client
*client
)
497 ret
= blinkm_transfer_hw(client
, BLM_STOP_SCRIPT
);
498 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
501 static int blinkm_test_run(struct i2c_client
*client
)
504 struct blinkm_data
*data
= i2c_get_clientdata(client
);
506 data
->next_red
= 0x01;
507 data
->next_green
= 0x05;
508 data
->next_blue
= 0x10;
509 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
514 data
->next_red
= 0x25;
515 data
->next_green
= 0x10;
516 data
->next_blue
= 0x31;
517 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
522 data
->next_hue
= 0x50;
523 data
->next_saturation
= 0x10;
524 data
->next_brightness
= 0x20;
525 ret
= blinkm_transfer_hw(client
, BLM_FADE_HSB
);
533 /* Return 0 if detection is successful, -ENODEV otherwise */
534 static int blinkm_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
536 struct i2c_adapter
*adapter
= client
->adapter
;
541 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
542 | I2C_FUNC_SMBUS_WORD_DATA
543 | I2C_FUNC_SMBUS_WRITE_BYTE
))
546 /* Now, we do the remaining detection. Simple for now. */
547 /* We might need more guards to protect other i2c slaves */
549 /* make sure the blinkM is balanced (read/writes) */
551 ret
= blinkm_write(client
, BLM_GET_ADDR
, NULL
);
552 usleep_range(5000, 10000);
553 ret
= blinkm_read(client
, BLM_GET_ADDR
, tmpargs
);
554 usleep_range(5000, 10000);
555 if (tmpargs
[0] == 0x09)
560 /* Step 1: Read BlinkM address back - cmd_char 'a' */
561 ret
= blinkm_write(client
, BLM_GET_ADDR
, NULL
);
564 usleep_range(20000, 30000); /* allow a small delay */
565 ret
= blinkm_read(client
, BLM_GET_ADDR
, tmpargs
);
569 if (tmpargs
[0] != 0x09) {
570 dev_err(&client
->dev
, "enodev DEV ADDR = 0x%02X\n", tmpargs
[0]);
574 strlcpy(info
->type
, "blinkm", I2C_NAME_SIZE
);
578 static int blinkm_probe(struct i2c_client
*client
,
579 const struct i2c_device_id
*id
)
581 struct blinkm_data
*data
;
582 struct blinkm_led
*led
[3];
584 char blinkm_led_name
[28];
586 data
= devm_kzalloc(&client
->dev
,
587 sizeof(struct blinkm_data
), GFP_KERNEL
);
593 data
->i2c_addr
= 0x09;
594 data
->i2c_addr
= 0x08;
595 /* i2c addr - use fake addr of 0x08 initially (real is 0x09) */
597 /* firmware version - use fake until we read real value
598 * (currently broken - BlinkM confused!) */
599 data
->script_id
= 0x01;
600 data
->i2c_client
= client
;
602 i2c_set_clientdata(client
, data
);
603 mutex_init(&data
->update_lock
);
605 /* Register sysfs hooks */
606 err
= sysfs_create_group(&client
->dev
.kobj
, &blinkm_group
);
608 dev_err(&client
->dev
, "couldn't register sysfs group\n");
612 for (i
= 0; i
< 3; i
++) {
613 /* RED = 0, GREEN = 1, BLUE = 2 */
614 led
[i
] = &data
->blinkm_leds
[i
];
615 led
[i
]->i2c_client
= client
;
617 led
[i
]->led_cdev
.max_brightness
= 255;
618 led
[i
]->led_cdev
.flags
= LED_CORE_SUSPENDRESUME
;
621 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
625 led
[i
]->led_cdev
.name
= blinkm_led_name
;
626 led
[i
]->led_cdev
.brightness_set_blocking
=
628 err
= led_classdev_register(&client
->dev
,
631 dev_err(&client
->dev
,
632 "couldn't register LED %s\n",
633 led
[i
]->led_cdev
.name
);
638 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
639 "blinkm-%d-%d-green",
642 led
[i
]->led_cdev
.name
= blinkm_led_name
;
643 led
[i
]->led_cdev
.brightness_set_blocking
=
644 blinkm_led_green_set
;
645 err
= led_classdev_register(&client
->dev
,
648 dev_err(&client
->dev
,
649 "couldn't register LED %s\n",
650 led
[i
]->led_cdev
.name
);
655 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
659 led
[i
]->led_cdev
.name
= blinkm_led_name
;
660 led
[i
]->led_cdev
.brightness_set_blocking
=
662 err
= led_classdev_register(&client
->dev
,
665 dev_err(&client
->dev
,
666 "couldn't register LED %s\n",
667 led
[i
]->led_cdev
.name
);
674 /* Initialize the blinkm */
675 blinkm_init_hw(client
);
680 led_classdev_unregister(&led
[GREEN
]->led_cdev
);
683 led_classdev_unregister(&led
[RED
]->led_cdev
);
686 sysfs_remove_group(&client
->dev
.kobj
, &blinkm_group
);
691 static int blinkm_remove(struct i2c_client
*client
)
693 struct blinkm_data
*data
= i2c_get_clientdata(client
);
697 /* make sure no workqueue entries are pending */
698 for (i
= 0; i
< 3; i
++)
699 led_classdev_unregister(&data
->blinkm_leds
[i
].led_cdev
);
702 data
->next_red
= 0x00;
703 data
->next_green
= 0x00;
704 data
->next_blue
= 0x00;
705 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
707 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
710 data
->next_hue
= 0x00;
711 data
->next_saturation
= 0x00;
712 data
->next_brightness
= 0x00;
713 ret
= blinkm_transfer_hw(client
, BLM_FADE_HSB
);
715 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
717 /* red fade to off */
718 data
->next_red
= 0xff;
719 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
721 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
724 data
->next_red
= 0x00;
725 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
727 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
729 sysfs_remove_group(&client
->dev
.kobj
, &blinkm_group
);
733 static const struct i2c_device_id blinkm_id
[] = {
738 MODULE_DEVICE_TABLE(i2c
, blinkm_id
);
740 /* This is the driver that will be inserted */
741 static struct i2c_driver blinkm_driver
= {
742 .class = I2C_CLASS_HWMON
,
746 .probe
= blinkm_probe
,
747 .remove
= blinkm_remove
,
748 .id_table
= blinkm_id
,
749 .detect
= blinkm_detect
,
750 .address_list
= normal_i2c
,
753 module_i2c_driver(blinkm_driver
);
755 MODULE_AUTHOR("Jan-Simon Moeller <dl9pf@gmx.de>");
756 MODULE_DESCRIPTION("BlinkM RGB LED driver");
757 MODULE_LICENSE("GPL");