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/init.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/err.h>
26 #include <linux/mutex.h>
27 #include <linux/sysfs.h>
28 #include <linux/printk.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/leds.h>
31 #include <linux/delay.h>
33 /* Addresses to scan - BlinkM is on 0x09 by default*/
34 static const unsigned short normal_i2c
[] = { 0x09, I2C_CLIENT_END
};
36 static int blinkm_transfer_hw(struct i2c_client
*client
, int cmd
);
37 static int blinkm_test_run(struct i2c_client
*client
);
40 struct i2c_client
*i2c_client
;
41 struct led_classdev led_cdev
;
47 struct blinkm_led
*blinkm_led
;
48 struct work_struct work
;
51 #define cdev_to_blmled(c) container_of(c, struct blinkm_led, led_cdev)
52 #define work_to_blmwork(c) container_of(c, struct blinkm_work, work)
55 struct i2c_client
*i2c_client
;
56 struct mutex update_lock
;
57 /* used for led class interface */
58 struct blinkm_led blinkm_leds
[3];
59 /* used for "blinkm" sysfs interface */
60 u8 red
; /* color red */
61 u8 green
; /* color green */
62 u8 blue
; /* color blue */
63 /* next values to use for transfer */
64 u8 next_red
; /* color red */
65 u8 next_green
; /* color green */
66 u8 next_blue
; /* color blue */
68 u8 args
[7]; /* set of args for transmission */
69 u8 i2c_addr
; /* i2c addr */
70 u8 fw_ver
; /* firmware version */
71 /* used, but not from userspace */
73 u8 saturation
; /* HSB saturation */
74 u8 brightness
; /* HSB brightness */
75 u8 next_hue
; /* HSB hue */
76 u8 next_saturation
; /* HSB saturation */
77 u8 next_brightness
; /* HSB brightness */
78 /* currently unused / todo */
79 u8 fade_speed
; /* fade speed 1 - 255 */
80 s8 time_adjust
; /* time adjust -128 - 127 */
81 u8 fade
:1; /* fade on = 1, off = 0 */
82 u8 rand
:1; /* rand fade mode on = 1 */
83 u8 script_id
; /* script ID */
84 u8 script_repeats
; /* repeats of script */
85 u8 script_startline
; /* line to start */
93 /* mapping command names to cmd chars - see datasheet */
95 #define BLM_FADE_RGB 1
96 #define BLM_FADE_HSB 2
97 #define BLM_FADE_RAND_RGB 3
98 #define BLM_FADE_RAND_HSB 4
99 #define BLM_PLAY_SCRIPT 5
100 #define BLM_STOP_SCRIPT 6
101 #define BLM_SET_FADE_SPEED 7
102 #define BLM_SET_TIME_ADJ 8
103 #define BLM_GET_CUR_RGB 9
104 #define BLM_WRITE_SCRIPT_LINE 10
105 #define BLM_READ_SCRIPT_LINE 11
106 #define BLM_SET_SCRIPT_LR 12 /* Length & Repeats */
107 #define BLM_SET_ADDR 13
108 #define BLM_GET_ADDR 14
109 #define BLM_GET_FW_VER 15
110 #define BLM_SET_STARTUP_PARAM 16
113 * as extracted out of the datasheet:
115 * cmdchar = command (ascii)
116 * cmdbyte = command in hex
117 * nr_args = number of arguments (to send)
118 * nr_ret = number of return values (to read)
119 * dir = direction (0 = read, 1 = write, 2 = both)
122 static const struct {
128 } blinkm_cmds
[17] = {
129 /* cmdchar, cmdbyte, nr_args, nr_ret, dir */
130 { 'n', 0x6e, 3, 0, 1},
131 { 'c', 0x63, 3, 0, 1},
132 { 'h', 0x68, 3, 0, 1},
133 { 'C', 0x43, 3, 0, 1},
134 { 'H', 0x48, 3, 0, 1},
135 { 'p', 0x70, 3, 0, 1},
136 { 'o', 0x6f, 0, 0, 1},
137 { 'f', 0x66, 1, 0, 1},
138 { 't', 0x74, 1, 0, 1},
139 { 'g', 0x67, 0, 3, 0},
140 { 'W', 0x57, 7, 0, 1},
141 { 'R', 0x52, 2, 5, 2},
142 { 'L', 0x4c, 3, 0, 1},
143 { 'A', 0x41, 4, 0, 1},
144 { 'a', 0x61, 0, 1, 0},
145 { 'Z', 0x5a, 0, 1, 0},
146 { 'B', 0x42, 5, 0, 1},
149 static ssize_t
show_color_common(struct device
*dev
, char *buf
, int color
)
151 struct i2c_client
*client
;
152 struct blinkm_data
*data
;
155 client
= to_i2c_client(dev
);
156 data
= i2c_get_clientdata(client
);
158 ret
= blinkm_transfer_hw(client
, BLM_GET_CUR_RGB
);
163 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->red
);
166 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->green
);
169 return scnprintf(buf
, PAGE_SIZE
, "%02X\n", data
->blue
);
177 static int store_color_common(struct device
*dev
, const char *buf
, int color
)
179 struct i2c_client
*client
;
180 struct blinkm_data
*data
;
184 client
= to_i2c_client(dev
);
185 data
= i2c_get_clientdata(client
);
187 ret
= kstrtou8(buf
, 10, &value
);
189 dev_err(dev
, "BlinkM: value too large!\n");
195 data
->next_red
= value
;
198 data
->next_green
= value
;
201 data
->next_blue
= value
;
207 dev_dbg(dev
, "next_red = %d, next_green = %d, next_blue = %d\n",
208 data
->next_red
, data
->next_green
, data
->next_blue
);
211 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
213 dev_err(dev
, "BlinkM: can't set RGB\n");
219 static ssize_t
show_red(struct device
*dev
, struct device_attribute
*attr
,
222 return show_color_common(dev
, buf
, RED
);
225 static ssize_t
store_red(struct device
*dev
, struct device_attribute
*attr
,
226 const char *buf
, size_t count
)
230 ret
= store_color_common(dev
, buf
, RED
);
236 static DEVICE_ATTR(red
, S_IRUGO
| S_IWUSR
, show_red
, store_red
);
238 static ssize_t
show_green(struct device
*dev
, struct device_attribute
*attr
,
241 return show_color_common(dev
, buf
, GREEN
);
244 static ssize_t
store_green(struct device
*dev
, struct device_attribute
*attr
,
245 const char *buf
, size_t count
)
250 ret
= store_color_common(dev
, buf
, GREEN
);
256 static DEVICE_ATTR(green
, S_IRUGO
| S_IWUSR
, show_green
, store_green
);
258 static ssize_t
show_blue(struct device
*dev
, struct device_attribute
*attr
,
261 return show_color_common(dev
, buf
, BLUE
);
264 static ssize_t
store_blue(struct device
*dev
, struct device_attribute
*attr
,
265 const char *buf
, size_t count
)
269 ret
= store_color_common(dev
, buf
, BLUE
);
275 static DEVICE_ATTR(blue
, S_IRUGO
| S_IWUSR
, show_blue
, store_blue
);
277 static ssize_t
show_test(struct device
*dev
, struct device_attribute
*attr
,
280 return scnprintf(buf
, PAGE_SIZE
,
281 "#Write into test to start test sequence!#\n");
284 static ssize_t
store_test(struct device
*dev
, struct device_attribute
*attr
,
285 const char *buf
, size_t count
)
288 struct i2c_client
*client
;
290 client
= to_i2c_client(dev
);
293 ret
= blinkm_test_run(client
);
300 static DEVICE_ATTR(test
, S_IRUGO
| S_IWUSR
, show_test
, store_test
);
302 /* TODO: HSB, fade, timeadj, script ... */
304 static struct attribute
*blinkm_attrs
[] = {
306 &dev_attr_green
.attr
,
312 static struct attribute_group blinkm_group
= {
314 .attrs
= blinkm_attrs
,
317 static int blinkm_write(struct i2c_client
*client
, int cmd
, u8
*arg
)
321 int arglen
= blinkm_cmds
[cmd
].nr_args
;
322 /* write out cmd to blinkm - always / default step */
323 result
= i2c_smbus_write_byte(client
, blinkm_cmds
[cmd
].cmdbyte
);
326 /* no args to write out */
330 for (i
= 0; i
< arglen
; i
++) {
331 /* repeat for arglen */
332 result
= i2c_smbus_write_byte(client
, arg
[i
]);
339 static int blinkm_read(struct i2c_client
*client
, int cmd
, u8
*arg
)
343 int retlen
= blinkm_cmds
[cmd
].nr_ret
;
344 for (i
= 0; i
< retlen
; i
++) {
345 /* repeat for retlen */
346 result
= i2c_smbus_read_byte(client
);
355 static int blinkm_transfer_hw(struct i2c_client
*client
, int cmd
)
357 /* the protocol is simple but non-standard:
358 * e.g. cmd 'g' (= 0x67) for "get device address"
359 * - which defaults to 0x09 - would be the sequence:
360 * a) write 0x67 to the device (byte write)
361 * b) read the value (0x09) back right after (byte read)
363 * Watch out for "unfinished" sequences (i.e. not enough reads
364 * or writes after a command. It will make the blinkM misbehave.
365 * Sequence is key here.
368 /* args / return are in private data struct */
369 struct blinkm_data
*data
= i2c_get_clientdata(client
);
371 /* We start hardware transfers which are not to be
372 * mixed with other commands. Aquire a lock now. */
373 if (mutex_lock_interruptible(&data
->update_lock
) < 0)
376 /* switch cmd - usually write before reads */
378 case BLM_FADE_RAND_RGB
:
381 data
->args
[0] = data
->next_red
;
382 data
->args
[1] = data
->next_green
;
383 data
->args
[2] = data
->next_blue
;
384 blinkm_write(client
, cmd
, data
->args
);
385 data
->red
= data
->args
[0];
386 data
->green
= data
->args
[1];
387 data
->blue
= data
->args
[2];
390 case BLM_FADE_RAND_HSB
:
391 data
->args
[0] = data
->next_hue
;
392 data
->args
[1] = data
->next_saturation
;
393 data
->args
[2] = data
->next_brightness
;
394 blinkm_write(client
, cmd
, data
->args
);
395 data
->hue
= data
->next_hue
;
396 data
->saturation
= data
->next_saturation
;
397 data
->brightness
= data
->next_brightness
;
399 case BLM_PLAY_SCRIPT
:
400 data
->args
[0] = data
->script_id
;
401 data
->args
[1] = data
->script_repeats
;
402 data
->args
[2] = data
->script_startline
;
403 blinkm_write(client
, cmd
, data
->args
);
405 case BLM_STOP_SCRIPT
:
406 blinkm_write(client
, cmd
, NULL
);
408 case BLM_GET_CUR_RGB
:
409 data
->args
[0] = data
->red
;
410 data
->args
[1] = data
->green
;
411 data
->args
[2] = data
->blue
;
412 blinkm_write(client
, cmd
, NULL
);
413 blinkm_read(client
, cmd
, data
->args
);
414 data
->red
= data
->args
[0];
415 data
->green
= data
->args
[1];
416 data
->blue
= data
->args
[2];
419 data
->args
[0] = data
->i2c_addr
;
420 blinkm_write(client
, cmd
, NULL
);
421 blinkm_read(client
, cmd
, data
->args
);
422 data
->i2c_addr
= data
->args
[0];
424 case BLM_SET_TIME_ADJ
:
425 case BLM_SET_FADE_SPEED
:
426 case BLM_READ_SCRIPT_LINE
:
427 case BLM_WRITE_SCRIPT_LINE
:
428 case BLM_SET_SCRIPT_LR
:
431 case BLM_SET_STARTUP_PARAM
:
432 dev_err(&client
->dev
,
433 "BlinkM: cmd %d not implemented yet.\n", cmd
);
436 dev_err(&client
->dev
, "BlinkM: unknown command %d\n", cmd
);
437 mutex_unlock(&data
->update_lock
);
439 } /* end switch(cmd) */
441 /* transfers done, unlock */
442 mutex_unlock(&data
->update_lock
);
446 static void led_work(struct work_struct
*work
)
449 struct blinkm_led
*led
;
450 struct blinkm_data
*data
;
451 struct blinkm_work
*blm_work
= work_to_blmwork(work
);
453 led
= blm_work
->blinkm_led
;
454 data
= i2c_get_clientdata(led
->i2c_client
);
455 ret
= blinkm_transfer_hw(led
->i2c_client
, BLM_GO_RGB
);
456 atomic_dec(&led
->active
);
457 dev_dbg(&led
->i2c_client
->dev
,
458 "# DONE # 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
));
465 static int blinkm_led_common_set(struct led_classdev
*led_cdev
,
466 enum led_brightness value
, int color
)
468 /* led_brightness is 0, 127 or 255 - we just use it here as-is */
469 struct blinkm_led
*led
= cdev_to_blmled(led_cdev
);
470 struct blinkm_data
*data
= i2c_get_clientdata(led
->i2c_client
);
471 struct blinkm_work
*bl_work
;
475 /* bail out if there's no change */
476 if (data
->next_red
== (u8
) value
)
478 /* we assume a quite fast sequence here ([off]->on->off)
479 * think of network led trigger - we cannot blink that fast, so
480 * in case we already have a off->on->off transition queued up,
481 * we refuse to queue up more.
482 * Revisit: fast-changing brightness. */
483 if (atomic_read(&led
->active
) > 1)
485 data
->next_red
= (u8
) value
;
488 /* bail out if there's no change */
489 if (data
->next_green
== (u8
) value
)
491 /* we assume a quite fast sequence here ([off]->on->off)
492 * Revisit: fast-changing brightness. */
493 if (atomic_read(&led
->active
) > 1)
495 data
->next_green
= (u8
) value
;
498 /* bail out if there's no change */
499 if (data
->next_blue
== (u8
) value
)
501 /* we assume a quite fast sequence here ([off]->on->off)
502 * Revisit: fast-changing brightness. */
503 if (atomic_read(&led
->active
) > 1)
505 data
->next_blue
= (u8
) value
;
509 dev_err(&led
->i2c_client
->dev
, "BlinkM: unknown color.\n");
513 bl_work
= kzalloc(sizeof(*bl_work
), GFP_ATOMIC
);
517 atomic_inc(&led
->active
);
518 dev_dbg(&led
->i2c_client
->dev
,
519 "#TO_SCHED# next_red = %d, next_green = %d,"
520 " next_blue = %d, active = %d\n",
521 data
->next_red
, data
->next_green
,
522 data
->next_blue
, atomic_read(&led
->active
));
524 /* a fresh work _item_ for each change */
525 bl_work
->blinkm_led
= led
;
526 INIT_WORK(&bl_work
->work
, led_work
);
527 /* queue work in own queue for easy sync on exit*/
528 schedule_work(&bl_work
->work
);
533 static void blinkm_led_red_set(struct led_classdev
*led_cdev
,
534 enum led_brightness value
)
536 blinkm_led_common_set(led_cdev
, value
, RED
);
539 static void blinkm_led_green_set(struct led_classdev
*led_cdev
,
540 enum led_brightness value
)
542 blinkm_led_common_set(led_cdev
, value
, GREEN
);
545 static void blinkm_led_blue_set(struct led_classdev
*led_cdev
,
546 enum led_brightness value
)
548 blinkm_led_common_set(led_cdev
, value
, BLUE
);
551 static void blinkm_init_hw(struct i2c_client
*client
)
554 ret
= blinkm_transfer_hw(client
, BLM_STOP_SCRIPT
);
555 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
558 static int blinkm_test_run(struct i2c_client
*client
)
561 struct blinkm_data
*data
= i2c_get_clientdata(client
);
563 data
->next_red
= 0x01;
564 data
->next_green
= 0x05;
565 data
->next_blue
= 0x10;
566 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
571 data
->next_red
= 0x25;
572 data
->next_green
= 0x10;
573 data
->next_blue
= 0x31;
574 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
579 data
->next_hue
= 0x50;
580 data
->next_saturation
= 0x10;
581 data
->next_brightness
= 0x20;
582 ret
= blinkm_transfer_hw(client
, BLM_FADE_HSB
);
590 /* Return 0 if detection is successful, -ENODEV otherwise */
591 static int blinkm_detect(struct i2c_client
*client
, struct i2c_board_info
*info
)
593 struct i2c_adapter
*adapter
= client
->adapter
;
598 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
599 | I2C_FUNC_SMBUS_WORD_DATA
600 | I2C_FUNC_SMBUS_WRITE_BYTE
))
603 /* Now, we do the remaining detection. Simple for now. */
604 /* We might need more guards to protect other i2c slaves */
606 /* make sure the blinkM is balanced (read/writes) */
608 ret
= blinkm_write(client
, BLM_GET_ADDR
, NULL
);
609 usleep_range(5000, 10000);
610 ret
= blinkm_read(client
, BLM_GET_ADDR
, tmpargs
);
611 usleep_range(5000, 10000);
612 if (tmpargs
[0] == 0x09)
617 /* Step 1: Read BlinkM address back - cmd_char 'a' */
618 ret
= blinkm_write(client
, BLM_GET_ADDR
, NULL
);
621 usleep_range(20000, 30000); /* allow a small delay */
622 ret
= blinkm_read(client
, BLM_GET_ADDR
, tmpargs
);
626 if (tmpargs
[0] != 0x09) {
627 dev_err(&client
->dev
, "enodev DEV ADDR = 0x%02X\n", tmpargs
[0]);
631 strlcpy(info
->type
, "blinkm", I2C_NAME_SIZE
);
635 static int __devinit
blinkm_probe(struct i2c_client
*client
,
636 const struct i2c_device_id
*id
)
638 struct blinkm_data
*data
;
639 struct blinkm_led
*led
[3];
641 char blinkm_led_name
[28];
643 data
= devm_kzalloc(&client
->dev
,
644 sizeof(struct blinkm_data
), GFP_KERNEL
);
650 data
->i2c_addr
= 0x09;
651 data
->i2c_addr
= 0x08;
652 /* i2c addr - use fake addr of 0x08 initially (real is 0x09) */
654 /* firmware version - use fake until we read real value
655 * (currently broken - BlinkM confused!) */
656 data
->script_id
= 0x01;
657 data
->i2c_client
= client
;
659 i2c_set_clientdata(client
, data
);
660 mutex_init(&data
->update_lock
);
662 /* Register sysfs hooks */
663 err
= sysfs_create_group(&client
->dev
.kobj
, &blinkm_group
);
665 dev_err(&client
->dev
, "couldn't register sysfs group\n");
669 for (i
= 0; i
< 3; i
++) {
670 /* RED = 0, GREEN = 1, BLUE = 2 */
671 led
[i
] = &data
->blinkm_leds
[i
];
672 led
[i
]->i2c_client
= client
;
674 led
[i
]->led_cdev
.max_brightness
= 255;
675 led
[i
]->led_cdev
.flags
= LED_CORE_SUSPENDRESUME
;
676 atomic_set(&led
[i
]->active
, 0);
679 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
683 led
[i
]->led_cdev
.name
= blinkm_led_name
;
684 led
[i
]->led_cdev
.brightness_set
= blinkm_led_red_set
;
685 err
= led_classdev_register(&client
->dev
,
688 dev_err(&client
->dev
,
689 "couldn't register LED %s\n",
690 led
[i
]->led_cdev
.name
);
695 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
696 "blinkm-%d-%d-green",
699 led
[i
]->led_cdev
.name
= blinkm_led_name
;
700 led
[i
]->led_cdev
.brightness_set
= blinkm_led_green_set
;
701 err
= led_classdev_register(&client
->dev
,
704 dev_err(&client
->dev
,
705 "couldn't register LED %s\n",
706 led
[i
]->led_cdev
.name
);
711 snprintf(blinkm_led_name
, sizeof(blinkm_led_name
),
715 led
[i
]->led_cdev
.name
= blinkm_led_name
;
716 led
[i
]->led_cdev
.brightness_set
= blinkm_led_blue_set
;
717 err
= led_classdev_register(&client
->dev
,
720 dev_err(&client
->dev
,
721 "couldn't register LED %s\n",
722 led
[i
]->led_cdev
.name
);
729 /* Initialize the blinkm */
730 blinkm_init_hw(client
);
735 led_classdev_unregister(&led
[GREEN
]->led_cdev
);
738 led_classdev_unregister(&led
[RED
]->led_cdev
);
741 sysfs_remove_group(&client
->dev
.kobj
, &blinkm_group
);
746 static int __devexit
blinkm_remove(struct i2c_client
*client
)
748 struct blinkm_data
*data
= i2c_get_clientdata(client
);
752 /* make sure no workqueue entries are pending */
753 for (i
= 0; i
< 3; i
++) {
754 flush_scheduled_work();
755 led_classdev_unregister(&data
->blinkm_leds
[i
].led_cdev
);
759 data
->next_red
= 0x00;
760 data
->next_green
= 0x00;
761 data
->next_blue
= 0x00;
762 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
764 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
767 data
->next_hue
= 0x00;
768 data
->next_saturation
= 0x00;
769 data
->next_brightness
= 0x00;
770 ret
= blinkm_transfer_hw(client
, BLM_FADE_HSB
);
772 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
774 /* red fade to off */
775 data
->next_red
= 0xff;
776 ret
= blinkm_transfer_hw(client
, BLM_GO_RGB
);
778 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
781 data
->next_red
= 0x00;
782 ret
= blinkm_transfer_hw(client
, BLM_FADE_RGB
);
784 dev_err(&client
->dev
, "Failure in blinkm_remove ignored. Continuing.\n");
786 sysfs_remove_group(&client
->dev
.kobj
, &blinkm_group
);
790 static const struct i2c_device_id blinkm_id
[] = {
795 MODULE_DEVICE_TABLE(i2c
, blinkm_id
);
797 /* This is the driver that will be inserted */
798 static struct i2c_driver blinkm_driver
= {
799 .class = I2C_CLASS_HWMON
,
803 .probe
= blinkm_probe
,
804 .remove
= __devexit_p(blinkm_remove
),
805 .id_table
= blinkm_id
,
806 .detect
= blinkm_detect
,
807 .address_list
= normal_i2c
,
810 module_i2c_driver(blinkm_driver
);
812 MODULE_AUTHOR("Jan-Simon Moeller <dl9pf@gmx.de>");
813 MODULE_DESCRIPTION("BlinkM RGB LED driver");
814 MODULE_LICENSE("GPL");