25 #include <linux/module.h>
26 #include <linux/i2c.h>
28 #include <linux/sched.h>
31 #include <linux/input.h>
35 #include <linux/slab.h>
38 #define LM8323_CMD_READ_ID 0x80
39 #define LM8323_CMD_WRITE_CFG 0x81
40 #define LM8323_CMD_READ_INT 0x82
41 #define LM8323_CMD_RESET 0x83
42 #define LM8323_CMD_WRITE_PORT_SEL 0x85
43 #define LM8323_CMD_WRITE_PORT_STATE 0x86
44 #define LM8323_CMD_READ_PORT_SEL 0x87
45 #define LM8323_CMD_READ_PORT_STATE 0x88
46 #define LM8323_CMD_READ_FIFO 0x89
47 #define LM8323_CMD_RPT_READ_FIFO 0x8a
48 #define LM8323_CMD_SET_ACTIVE 0x8b
49 #define LM8323_CMD_READ_ERR 0x8c
50 #define LM8323_CMD_READ_ROTATOR 0x8e
51 #define LM8323_CMD_SET_DEBOUNCE 0x8f
52 #define LM8323_CMD_SET_KEY_SIZE 0x90
53 #define LM8323_CMD_READ_KEY_SIZE 0x91
54 #define LM8323_CMD_READ_CFG 0x92
55 #define LM8323_CMD_WRITE_CLOCK 0x93
56 #define LM8323_CMD_READ_CLOCK 0x94
57 #define LM8323_CMD_PWM_WRITE 0x95
58 #define LM8323_CMD_START_PWM 0x96
59 #define LM8323_CMD_STOP_PWM 0x97
62 #define INT_KEYPAD 0x01
63 #define INT_ROTATOR 0x02
64 #define INT_ERROR 0x08
65 #define INT_NOINIT 0x10
71 #define ERR_BADPAR 0x01
72 #define ERR_CMDUNK 0x02
73 #define ERR_KEYOVR 0x04
74 #define ERR_FIFOOVER 0x40
77 #define CFG_MUX1SEL 0x01
78 #define CFG_MUX1EN 0x02
79 #define CFG_MUX2SEL 0x04
80 #define CFG_MUX2EN 0x08
81 #define CFG_PSIZE 0x20
82 #define CFG_ROTEN 0x40
85 #define CLK_RCPWM_INTERNAL 0x00
86 #define CLK_RCPWM_EXTERNAL 0x03
87 #define CLK_SLOWCLKEN 0x08
88 #define CLK_SLOWCLKOUT 0x40
91 #define LM8323_I2C_ADDR00 (0x84 >> 1)
92 #define LM8323_I2C_ADDR01 (0x86 >> 1)
93 #define LM8323_I2C_ADDR10 (0x88 >> 1)
94 #define LM8323_I2C_ADDR11 (0x8A >> 1)
97 #define LM8323_FIFO_LEN 15
101 #define PWM_SET(v) (0x4000 | ((v) & 0xff))
103 #define PWM_GOTOSTART 0x0000
108 #define PWM_END(reset) (0xc000 | (!!(reset) << 11))
114 #define PWM_RAMP(s, t, n, u) ((!!(s) << 14) | ((t) & 0x3f) << 8 | \
115 ((n) & 0x7f) | ((u) ? 0 : 0x80))
120 #define PWM_LOOP(cnt, pos) (0xa000 | (((cnt) & 0x3f) << 7) | \
127 #define PWM_WAIT_TRIG(chans) (0xe000 | (((chans) & 0x7) << 6))
129 #define PWM_SEND_TRIG(chans) (0xe000 | ((chans) & 0x7))
162 #define client_to_lm8323(c) container_of(c, struct lm8323_chip, client)
163 #define dev_to_lm8323(d) container_of(d, struct lm8323_chip, client->dev)
164 #define cdev_to_pwm(c) container_of(c, struct lm8323_pwm, cdev)
165 #define work_to_pwm(w) container_of(w, struct lm8323_pwm, work)
167 #define LM8323_MAX_DATA 8
174 static int lm8323_write(
struct lm8323_chip *
lm,
int len, ...)
188 for (i = 0; i < len; i++)
189 data[i] =
va_arg(ap,
int);
223 dev_err(&lm->
client->dev,
"sending read cmd 0x%02x failed\n",
248 static inline u8 lm8323_whichkey(
u8 event)
253 static inline int lm8323_ispress(
u8 event)
255 return (event & 0x80) ? 1 : 0;
278 while ((event = key_fifo[i++])) {
279 u8 key = lm8323_whichkey(event);
280 int isdown = lm8323_ispress(event);
284 key, isdown ?
"down" :
"up");
288 input_report_key(lm->
idev, keycode, isdown);
289 input_sync(lm->
idev);
304 lm8323_set_active_time(lm, 0);
309 static void lm8323_process_error(
struct lm8323_chip *lm)
318 "more than two keys pressed\n");
321 "unknown command submitted\n");
333 static int lm8323_configure(
struct lm8323_chip *lm)
344 if (debounce >= active)
345 active = debounce + 3;
393 lm8323_process_error(lm);
398 lm8323_configure(lm);
403 "pwm%d engine completed\n", i);
404 pwm_done(&lm->
pwm[i]);
431 (cmd & 0xff00) >> 8, cmd & 0x00ff);
440 static void lm8323_write_pwm(
struct lm8323_pwm *pwm,
int kill,
445 for (i = 0; i < len; i++)
446 lm8323_write_pwm_one(pwm, i, cmds[i]);
448 lm8323_write_pwm_one(pwm, i++,
PWM_END(kill));
456 int div512, perstep,
steps, hz,
up, kill;
479 if ((pwm->
fade_time / steps) > (32768 / 512)) {
487 perstep = (hz * pwm->
fade_time) / (steps * 1000);
491 else if (perstep > 63)
498 pwm_cmds[num_cmds++] =
PWM_RAMP(div512, perstep, s, up);
502 lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
509 static void lm8323_pwm_set_brightness(
struct led_classdev *led_cdev,
529 lm8323_pwm_work(&pwm->
work);
559 static DEVICE_ATTR(time, 0644, lm8323_pwm_show_time, lm8323_pwm_store_time);
568 pwm = &lm->
pwm[
id - 1];
582 pwm->
cdev.brightness_set = lm8323_pwm_set_brightness;
584 dev_err(dev,
"couldn't register PWM %d\n",
id);
588 &dev_attr_time) < 0) {
589 dev_err(dev,
"couldn't register time attribute\n");
611 const char *buf,
size_t count)
625 static DEVICE_ATTR(disable_kp, 0644, lm8323_show_disable, lm8323_set_disable);
631 struct input_dev *
idev;
639 dev_err(&client->
dev,
"missing platform_data\n");
644 dev_err(&client->
dev,
"invalid x size %d specified\n",
650 dev_err(&client->
dev,
"invalid y size %d specified\n",
656 idev = input_allocate_device();
680 if (data[0] & INT_NOINIT)
685 "timeout waiting for initialisation\n");
692 lm8323_configure(lm);
695 if (lm8323_read_id(lm, data) != 0) {
702 err = init_pwm(lm, pwm + 1, &client->
dev,
713 idev->name = pdata->
name ? :
"LM8323 keypad";
715 "%s/input-kp", dev_name(&client->
dev));
716 idev->phys = lm->
phys;
729 err = input_register_device(idev);
731 dev_dbg(&client->
dev,
"error registering input device\n");
738 dev_err(&client->
dev,
"could not get IRQ %d\n", client->
irq);
742 i2c_set_clientdata(client, lm);
745 enable_irq_wake(client->
irq);
750 input_unregister_device(idev);
756 if (lm->
pwm[pwm].enabled) {
762 input_free_device(idev);
769 struct lm8323_chip *lm = i2c_get_clientdata(client);
772 disable_irq_wake(client->
irq);
775 input_unregister_device(lm->
idev);
779 for (i = 0; i < 3; i++)
780 if (lm->
pwm[i].enabled) {
790 #ifdef CONFIG_PM_SLEEP
795 static int lm8323_suspend(
struct device *dev)
798 struct lm8323_chip *lm = i2c_get_clientdata(client);
808 for (i = 0; i < 3; i++)
809 if (lm->
pwm[i].enabled)
815 static int lm8323_resume(
struct device *dev)
818 struct lm8323_chip *lm = i2c_get_clientdata(client);
825 for (i = 0; i < 3; i++)
826 if (lm->
pwm[i].enabled)
843 static struct i2c_driver lm8323_i2c_driver = {
846 .pm = &lm8323_pm_ops,
848 .probe = lm8323_probe,
850 .id_table = lm8323_id,