2 * arch/ubicom32/mach-common/ubicom32hid.c
3 * I2C driver for HID coprocessor found on some DPF implementations.
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
23 * Ubicom32 implementation derived from (with many thanks):
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/gpio.h>
32 #include <linux/delay.h>
33 #include <linux/platform_device.h>
34 #include <linux/i2c.h>
35 #include <linux/backlight.h>
37 #include <linux/input.h>
38 #include <linux/input-polldev.h>
40 #include <asm/ubicom32hid.h>
42 #define DRIVER_NAME "ubicom32hid"
45 static int ubicom32hid_debug
;
48 static const struct i2c_device_id ubicom32hid_id
[] = {
52 MODULE_DEVICE_TABLE(i2c
, ubicom32hid_id
);
55 * Define this to make IR checking strict, in general, it's not needed
57 #undef UBICOM32HID_STRICT_IR_CHECK
59 #define UBICOM32HID_CMD_SET_PWM 0x01
60 #define UBICOM32HID_CMD_SET_BL_EN 0x02
61 #define UBICOM32HID_BL_EN_LOW 0x00
62 #define UBICOM32HID_BL_EN_HIZ 0x01
63 #define UBICOM32HID_BL_EN_HI 0x02
64 #define UBICOM32HID_CMD_FLUSH 0x99
65 #define UBICOM32HID_CMD_RESET 0x99
66 #define UBICOM32HID_CMD_GET_IR_SWITCH 0xC0
67 #define UBICOM32HID_CMD_GET_REVISION 0xfd
68 #define UBICOM32HID_CMD_GET_DEVICE_ID 0xfe
69 #define UBICOM32HID_CMD_GET_VERSION 0xff
70 #define UBICOM32HID_DEVICE_ID 0x49
72 #define UBICOM32HID_MAX_BRIGHTNESS_PWM 255
75 * Data structure returned by the HID device
77 struct ubicom32hid_input_data
{
86 struct ubicom32hid_data
{
88 * Pointer to the platform data structure, we need the settings.
90 const struct ubicom32hid_platform_data
*pdata
;
95 struct backlight_device
*bldev
;
98 * I2C client, for sending messages to the HID device
100 struct i2c_client
*client
;
103 * Current intensity, used for get_intensity.
109 * We won't register an input subsystem if there are no mappings.
111 struct input_polled_dev
*poll_dev
;
116 * ubicom32hid_set_intensity
118 static int ubicom32hid_set_intensity(struct backlight_device
*bd
)
120 struct ubicom32hid_data
*ud
=
121 (struct ubicom32hid_data
*)bl_get_data(bd
);
122 int intensity
= bd
->props
.brightness
;
128 * If we're blanked the the intensity doesn't matter.
130 if ((bd
->props
.power
!= FB_BLANK_UNBLANK
) ||
131 (bd
->props
.fb_blank
!= FB_BLANK_UNBLANK
)) {
136 * Set the brightness based on the type of backlight
138 if (ud
->pdata
->type
== UBICOM32HID_BL_TYPE_BINARY
) {
139 reg
= UBICOM32HID_CMD_SET_BL_EN
;
141 val
= ud
->pdata
->invert
142 ? UBICOM32HID_BL_EN_LOW
: UBICOM32HID_BL_EN_HI
;
144 val
= ud
->pdata
->invert
145 ? UBICOM32HID_BL_EN_HI
: UBICOM32HID_BL_EN_LOW
;
148 reg
= UBICOM32HID_CMD_SET_PWM
;
149 val
= ud
->pdata
->invert
150 ? (UBICOM32HID_MAX_BRIGHTNESS_PWM
- intensity
) :
157 ret
= i2c_smbus_write_byte_data(ud
->client
, reg
, val
);
159 dev_warn(&ud
->client
->dev
, "Unable to write backlight err=%d\n",
164 ud
->cur_intensity
= intensity
;
170 * ubicom32hid_get_intensity
171 * Return the current intensity of the backlight.
173 static int ubicom32hid_get_intensity(struct backlight_device
*bd
)
175 struct ubicom32hid_data
*ud
=
176 (struct ubicom32hid_data
*)bl_get_data(bd
);
178 return ud
->cur_intensity
;
182 * ubicom32hid_verify_data
183 * Verify the data to see if there is any action to be taken
185 * Returns 0 if no action is to be taken, non-zero otherwise
187 static int ubicom32hid_verify_data(struct ubicom32hid_data
*ud
,
188 struct ubicom32hid_input_data
*data
)
190 uint8_t *ircmd
= (uint8_t *)&(data
->ircmd
);
193 * ircmd == DEADBEEF means ir queue is empty. Since this is a
194 * meaningful code, that means the rest of the message is most likely
195 * correct, so only process the data if the switch state has changed.
197 if (data
->ircmd
== 0xDEADBEEF) {
198 return data
->sw_changed
!= 0;
202 * We have an ircmd which is not empty:
203 * Data[1] should be the complement of Data[0]
205 if (ircmd
[0] != (u8_t
)~ircmd
[1]) {
209 #ifdef UBICOM32HID_STRICT_IR_CHECK
211 * It seems that some remote controls don't follow the NEC protocol
212 * properly, so only do this check if the remote does indeed follow the
213 * spec. Data[3] should be the complement of Data[2]
215 if (ircmd
[2] == (u8_t
)~ircmd
[3]) {
220 * For non-compliant remotes, check the system code according to what
223 if ((ircmd
[2] != UBICOM32HID_IR_SYSTEM_CODE_CHECK
) ||
224 (ircmd
[3] != UBICOM32HID_IR_SYSTEM_CODE
)) {
230 * Data checks out, process
236 * ubicom32hid_poll_input
237 * Poll the input from the HID device.
239 static void ubicom32hid_poll_input(struct input_polled_dev
*dev
)
241 struct ubicom32hid_data
*ud
= (struct ubicom32hid_data
*)dev
->private;
242 const struct ubicom32hid_platform_data
*pdata
= ud
->pdata
;
243 struct ubicom32hid_input_data data
;
244 struct input_dev
*id
= dev
->input
;
253 cmd
= UBICOM32HID_CMD_FLUSH
;
254 ret
= i2c_master_send(ud
->client
, &cmd
, 1);
259 ret
= i2c_smbus_read_i2c_block_data(
260 ud
->client
, UBICOM32HID_CMD_GET_IR_SWITCH
, 6, (void *)&data
);
266 * Verify the data to see if there is any action to be taken
268 if (!ubicom32hid_verify_data(ud
, &data
)) {
273 if (ubicom32hid_debug
) {
274 printk("Polled ircmd=%8x swstate=%2x swchanged=%2x\n",
275 data
.ircmd
, data
.sw_state
, data
.sw_changed
);
280 * Process changed switches
282 if (data
.sw_changed
) {
283 const struct ubicom32hid_button
*ub
= pdata
->buttons
;
284 for (i
= 0; i
< pdata
->nbuttons
; i
++, ub
++) {
285 uint8_t mask
= (1 << ub
->bit
);
286 if (!(data
.sw_changed
& mask
)) {
291 input_event(id
, ub
->type
, ub
->code
,
292 (data
.sw_state
& mask
) ? 1 : 0);
302 if (data
.ircmd
!= 0xDEADBEEF) {
303 const struct ubicom32hid_ir
*ui
= pdata
->ircodes
;
304 for (i
= 0; i
< pdata
->nircodes
; i
++, ui
++) {
305 if (ui
->ir_code
== data
.ircmd
) {
307 * Simulate a up/down event
309 input_event(id
, ui
->type
, ui
->code
, 1);
311 input_event(id
, ui
->type
, ui
->code
, 0);
322 static struct backlight_ops ubicom32hid_blops
= {
323 .get_brightness
= ubicom32hid_get_intensity
,
324 .update_status
= ubicom32hid_set_intensity
,
330 static int ubicom32hid_probe(struct i2c_client
*client
,
331 const struct i2c_device_id
*id
)
333 struct ubicom32hid_platform_data
*pdata
;
334 struct ubicom32hid_data
*ud
;
340 pdata
= client
->dev
.platform_data
;
346 * See if we even have a device available before allocating memory.
348 * Hard reset the device
350 ret
= gpio_request(pdata
->gpio_reset
, "ubicom32hid-reset");
354 gpio_direction_output(pdata
->gpio_reset
, pdata
->gpio_reset_polarity
);
356 gpio_set_value(pdata
->gpio_reset
, !pdata
->gpio_reset_polarity
);
360 * soft reset the device. It sometimes takes a while to do this.
362 for (i
= 0; i
< 50; i
++) {
363 buf
[0] = UBICOM32HID_CMD_RESET
;
364 ret
= i2c_master_send(client
, buf
, 1);
371 dev_warn(&client
->dev
, "Unable to reset device\n");
375 ret
= i2c_smbus_read_byte_data(client
, UBICOM32HID_CMD_GET_DEVICE_ID
);
376 if (ret
!= UBICOM32HID_DEVICE_ID
) {
377 dev_warn(&client
->dev
, "Incorrect device id %02x\n", buf
[0]);
382 ret
= i2c_smbus_read_byte_data(client
, UBICOM32HID_CMD_GET_VERSION
);
384 dev_warn(&client
->dev
, "Unable to get version\n");
389 ret
= i2c_smbus_read_byte_data(client
, UBICOM32HID_CMD_GET_REVISION
);
391 dev_warn(&client
->dev
, "Unable to get revision\n");
397 * Allocate our private data
399 ud
= kzalloc(sizeof(struct ubicom32hid_data
), GFP_KERNEL
);
408 * Register our backlight device
410 ud
->bldev
= backlight_device_register(DRIVER_NAME
, &client
->dev
,
411 ud
, &ubicom32hid_blops
);
412 if (IS_ERR(ud
->bldev
)) {
413 ret
= PTR_ERR(ud
->bldev
);
416 platform_set_drvdata(client
, ud
);
419 * Start up the backlight with the requested intensity
421 ud
->bldev
->props
.power
= FB_BLANK_UNBLANK
;
422 ud
->bldev
->props
.max_brightness
=
423 (pdata
->type
== UBICOM32HID_BL_TYPE_PWM
) ?
424 UBICOM32HID_MAX_BRIGHTNESS_PWM
: 1;
425 if (pdata
->default_intensity
< ud
->bldev
->props
.max_brightness
) {
426 ud
->bldev
->props
.brightness
= pdata
->default_intensity
;
428 dev_warn(&client
->dev
, "Default brightness out of range, "
430 ud
->bldev
->props
.brightness
= ud
->bldev
->props
.max_brightness
;
433 ubicom32hid_set_intensity(ud
->bldev
);
436 * Check to see if we have any inputs
438 if (!pdata
->nbuttons
&& !pdata
->nircodes
) {
443 * We have buttons or codes, we must register an input device
445 ud
->poll_dev
= input_allocate_polled_device();
452 * Setup the polling to default to 100ms
454 ud
->poll_dev
->poll
= ubicom32hid_poll_input
;
455 ud
->poll_dev
->poll_interval
=
456 pdata
->poll_interval
? pdata
->poll_interval
: 100;
457 ud
->poll_dev
->private = ud
;
459 ud
->poll_dev
->input
->name
=
460 pdata
->input_name
? pdata
->input_name
: "Ubicom32HID";
461 ud
->poll_dev
->input
->phys
= "ubicom32hid/input0";
462 ud
->poll_dev
->input
->dev
.parent
= &client
->dev
;
463 ud
->poll_dev
->input
->id
.bustype
= BUS_I2C
;
466 * Set the capabilities by running through the buttons and ir codes
468 for (i
= 0; i
< pdata
->nbuttons
; i
++) {
469 const struct ubicom32hid_button
*ub
= &pdata
->buttons
[i
];
471 input_set_capability(ud
->poll_dev
->input
,
472 ub
->type
? ub
->type
: EV_KEY
, ub
->code
);
475 for (i
= 0; i
< pdata
->nircodes
; i
++) {
476 const struct ubicom32hid_ir
*ui
= &pdata
->ircodes
[i
];
478 input_set_capability(ud
->poll_dev
->input
,
479 ui
->type
? ui
->type
: EV_KEY
, ui
->code
);
482 ret
= input_register_polled_device(ud
->poll_dev
);
488 printk(KERN_INFO DRIVER_NAME
": enabled, version=%02x.%02x\n",
489 version
[0], version
[1]);
494 gpio_free(ud
->pdata
->gpio_reset
);
495 backlight_device_unregister(ud
->bldev
);
499 gpio_free(pdata
->gpio_reset
);
506 static int ubicom32hid_remove(struct i2c_client
*client
)
508 struct ubicom32hid_data
*ud
=
509 (struct ubicom32hid_data
*)platform_get_drvdata(client
);
511 gpio_free(ud
->pdata
->gpio_reset
);
513 backlight_device_unregister(ud
->bldev
);
516 input_unregister_polled_device(ud
->poll_dev
);
517 input_free_polled_device(ud
->poll_dev
);
520 platform_set_drvdata(client
, NULL
);
527 static struct i2c_driver ubicom32hid_driver
= {
530 .owner
= THIS_MODULE
,
532 .probe
= ubicom32hid_probe
,
533 .remove
= __exit_p(ubicom32hid_remove
),
534 .id_table
= ubicom32hid_id
,
540 static int __init
ubicom32hid_init(void)
542 return i2c_add_driver(&ubicom32hid_driver
);
544 module_init(ubicom32hid_init
);
549 static void __exit
ubicom32hid_exit(void)
551 i2c_del_driver(&ubicom32hid_driver
);
553 module_exit(ubicom32hid_exit
);
555 MODULE_AUTHOR("Pat Tjin <@ubicom.com>")
556 MODULE_DESCRIPTION("Ubicom HID driver");
557 MODULE_LICENSE("GPL");