1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/i2c/chips/lm8323.c
4 *
5 * Copyright (C) 2007-2009 Nokia Corporation
6 *
7 * Written by Daniel Stone <daniel.stone@nokia.com>
8 * Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
9 *
10 * Updated by Felipe Balbi <felipe.balbi@nokia.com>
11 */
12
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/sched.h>
17 #include <linux/mutex.h>
18 #include <linux/delay.h>
19 #include <linux/input.h>
20 #include <linux/leds.h>
21 #include <linux/platform_data/lm8323.h>
22 #include <linux/pm.h>
23 #include <linux/slab.h>
24
25 /* Commands to send to the chip. */
26 #define LM8323_CMD_READ_ID 0x80 /* Read chip ID. */
27 #define LM8323_CMD_WRITE_CFG 0x81 /* Set configuration item. */
28 #define LM8323_CMD_READ_INT 0x82 /* Get interrupt status. */
29 #define LM8323_CMD_RESET 0x83 /* Reset, same as external one */
30 #define LM8323_CMD_WRITE_PORT_SEL 0x85 /* Set GPIO in/out. */
31 #define LM8323_CMD_WRITE_PORT_STATE 0x86 /* Set GPIO pullup. */
32 #define LM8323_CMD_READ_PORT_SEL 0x87 /* Get GPIO in/out. */
33 #define LM8323_CMD_READ_PORT_STATE 0x88 /* Get GPIO pullup. */
34 #define LM8323_CMD_READ_FIFO 0x89 /* Read byte from FIFO. */
35 #define LM8323_CMD_RPT_READ_FIFO 0x8a /* Read FIFO (no increment). */
36 #define LM8323_CMD_SET_ACTIVE 0x8b /* Set active time. */
37 #define LM8323_CMD_READ_ERR 0x8c /* Get error status. */
38 #define LM8323_CMD_READ_ROTATOR 0x8e /* Read rotator status. */
39 #define LM8323_CMD_SET_DEBOUNCE 0x8f /* Set debouncing time. */
40 #define LM8323_CMD_SET_KEY_SIZE 0x90 /* Set keypad size. */
41 #define LM8323_CMD_READ_KEY_SIZE 0x91 /* Get keypad size. */
42 #define LM8323_CMD_READ_CFG 0x92 /* Get configuration item. */
43 #define LM8323_CMD_WRITE_CLOCK 0x93 /* Set clock config. */
44 #define LM8323_CMD_READ_CLOCK 0x94 /* Get clock config. */
45 #define LM8323_CMD_PWM_WRITE 0x95 /* Write PWM script. */
46 #define LM8323_CMD_START_PWM 0x96 /* Start PWM engine. */
47 #define LM8323_CMD_STOP_PWM 0x97 /* Stop PWM engine. */
48
49 /* Interrupt status. */
50 #define INT_KEYPAD 0x01 /* Key event. */
51 #define INT_ROTATOR 0x02 /* Rotator event. */
52 #define INT_ERROR 0x08 /* Error: use CMD_READ_ERR. */
53 #define INT_NOINIT 0x10 /* Lost configuration. */
54 #define INT_PWM1 0x20 /* PWM1 stopped. */
55 #define INT_PWM2 0x40 /* PWM2 stopped. */
56 #define INT_PWM3 0x80 /* PWM3 stopped. */
57
58 /* Errors (signalled by INT_ERROR, read with CMD_READ_ERR). */
59 #define ERR_BADPAR 0x01 /* Bad parameter. */
60 #define ERR_CMDUNK 0x02 /* Unknown command. */
61 #define ERR_KEYOVR 0x04 /* Too many keys pressed. */
62 #define ERR_FIFOOVER 0x40 /* FIFO overflow. */
63
64 /* Configuration keys (CMD_{WRITE,READ}_CFG). */
65 #define CFG_MUX1SEL 0x01 /* Select MUX1_OUT input. */
66 #define CFG_MUX1EN 0x02 /* Enable MUX1_OUT. */
67 #define CFG_MUX2SEL 0x04 /* Select MUX2_OUT input. */
68 #define CFG_MUX2EN 0x08 /* Enable MUX2_OUT. */
69 #define CFG_PSIZE 0x20 /* Package size (must be 0). */
70 #define CFG_ROTEN 0x40 /* Enable rotator. */
71
72 /* Clock settings (CMD_{WRITE,READ}_CLOCK). */
73 #define CLK_RCPWM_INTERNAL 0x00
74 #define CLK_RCPWM_EXTERNAL 0x03
75 #define CLK_SLOWCLKEN 0x08 /* Enable 32.768kHz clock. */
76 #define CLK_SLOWCLKOUT 0x40 /* Enable slow pulse output. */
77
78 /* The possible addresses corresponding to CONFIG1 and CONFIG2 pin wirings. */
79 #define LM8323_I2C_ADDR00 (0x84 >> 1) /* 1000 010x */
80 #define LM8323_I2C_ADDR01 (0x86 >> 1) /* 1000 011x */
81 #define LM8323_I2C_ADDR10 (0x88 >> 1) /* 1000 100x */
82 #define LM8323_I2C_ADDR11 (0x8A >> 1) /* 1000 101x */
83
84 /* Key event fifo length */
85 #define LM8323_FIFO_LEN 15
86
87 /* Commands for PWM engine; feed in with PWM_WRITE. */
88 /* Load ramp counter from duty cycle field (range 0 - 0xff). */
89 #define PWM_SET(v) (0x4000 | ((v) & 0xff))
90 /* Go to start of script. */
91 #define PWM_GOTOSTART 0x0000
92 /*
93 * Stop engine (generates interrupt). If reset is 1, clear the program
94 * counter, else leave it.
95 */
96 #define PWM_END(reset) (0xc000 | (!!(reset) << 11))
97 /*
98 * Ramp. If s is 1, divide clock by 512, else divide clock by 16.
99 * Take t clock scales (up to 63) per step, for n steps (up to 126).
100 * If u is set, ramp up, else ramp down.
101 */
102 #define PWM_RAMP(s, t, n, u) ((!!(s) << 14) | ((t) & 0x3f) << 8 | \
103 ((n) & 0x7f) | ((u) ? 0 : 0x80))
104 /*
105 * Loop (i.e. jump back to pos) for a given number of iterations (up to 63).
106 * If cnt is zero, execute until PWM_END is encountered.
107 */
108 #define PWM_LOOP(cnt, pos) (0xa000 | (((cnt) & 0x3f) << 7) | \
109 ((pos) & 0x3f))
110 /*
111 * Wait for trigger. Argument is a mask of channels, shifted by the channel
112 * number, e.g. 0xa for channels 3 and 1. Note that channels are numbered
113 * from 1, not 0.
114 */
115 #define PWM_WAIT_TRIG(chans) (0xe000 | (((chans) & 0x7) << 6))
116 /* Send trigger. Argument is same as PWM_WAIT_TRIG. */
117 #define PWM_SEND_TRIG(chans) (0xe000 | ((chans) & 0x7))
118
119 struct lm8323_pwm {
120 int id;
121 int fade_time;
122 int brightness;
123 int desired_brightness;
124 bool enabled;
125 bool running;
126 /* pwm lock */
127 struct mutex lock;
128 struct work_struct work;
129 struct led_classdev cdev;
130 struct lm8323_chip *chip;
131 };
132
133 struct lm8323_chip {
134 /* device lock */
135 struct mutex lock;
136 struct i2c_client *client;
137 struct input_dev *idev;
138 bool kp_enabled;
139 bool pm_suspend;
140 unsigned keys_down;
141 char phys[32];
142 unsigned short keymap[LM8323_KEYMAP_SIZE];
143 int size_x;
144 int size_y;
145 int debounce_time;
146 int active_time;
147 struct lm8323_pwm pwm[LM8323_NUM_PWMS];
148 };
149
150 #define client_to_lm8323(c) container_of(c, struct lm8323_chip, client)
151 #define dev_to_lm8323(d) container_of(d, struct lm8323_chip, client->dev)
152 #define cdev_to_pwm(c) container_of(c, struct lm8323_pwm, cdev)
153 #define work_to_pwm(w) container_of(w, struct lm8323_pwm, work)
154
155 #define LM8323_MAX_DATA 8
156
157 /*
158 * To write, we just access the chip's address in write mode, and dump the
159 * command and data out on the bus. The command byte and data are taken as
160 * sequential u8s out of varargs, to a maximum of LM8323_MAX_DATA.
161 */
lm8323_write(struct lm8323_chip * lm,int len,...)162 static int lm8323_write(struct lm8323_chip *lm, int len, ...)
163 {
164 int ret, i;
165 va_list ap;
166 u8 data[LM8323_MAX_DATA];
167
168 va_start(ap, len);
169
170 if (unlikely(len > LM8323_MAX_DATA)) {
171 dev_err(&lm->client->dev, "tried to send %d bytes\n", len);
172 va_end(ap);
173 return 0;
174 }
175
176 for (i = 0; i < len; i++)
177 data[i] = va_arg(ap, int);
178
179 va_end(ap);
180
181 /*
182 * If the host is asleep while we send the data, we can get a NACK
183 * back while it wakes up, so try again, once.
184 */
185 ret = i2c_master_send(lm->client, data, len);
186 if (unlikely(ret == -EREMOTEIO))
187 ret = i2c_master_send(lm->client, data, len);
188 if (unlikely(ret != len))
189 dev_err(&lm->client->dev, "sent %d bytes of %d total\n",
190 len, ret);
191
192 return ret;
193 }
194
195 /*
196 * To read, we first send the command byte to the chip and end the transaction,
197 * then access the chip in read mode, at which point it will send the data.
198 */
lm8323_read(struct lm8323_chip * lm,u8 cmd,u8 * buf,int len)199 static int lm8323_read(struct lm8323_chip *lm, u8 cmd, u8 *buf, int len)
200 {
201 int ret;
202
203 /*
204 * If the host is asleep while we send the byte, we can get a NACK
205 * back while it wakes up, so try again, once.
206 */
207 ret = i2c_master_send(lm->client, &cmd, 1);
208 if (unlikely(ret == -EREMOTEIO))
209 ret = i2c_master_send(lm->client, &cmd, 1);
210 if (unlikely(ret != 1)) {
211 dev_err(&lm->client->dev, "sending read cmd 0x%02x failed\n",
212 cmd);
213 return 0;
214 }
215
216 ret = i2c_master_recv(lm->client, buf, len);
217 if (unlikely(ret != len))
218 dev_err(&lm->client->dev, "wanted %d bytes, got %d\n",
219 len, ret);
220
221 return ret;
222 }
223
224 /*
225 * Set the chip active time (idle time before it enters halt).
226 */
lm8323_set_active_time(struct lm8323_chip * lm,int time)227 static void lm8323_set_active_time(struct lm8323_chip *lm, int time)
228 {
229 lm8323_write(lm, 2, LM8323_CMD_SET_ACTIVE, time >> 2);
230 }
231
232 /*
233 * The signals are AT-style: the low 7 bits are the keycode, and the top
234 * bit indicates the state (1 for down, 0 for up).
235 */
lm8323_whichkey(u8 event)236 static inline u8 lm8323_whichkey(u8 event)
237 {
238 return event & 0x7f;
239 }
240
lm8323_ispress(u8 event)241 static inline int lm8323_ispress(u8 event)
242 {
243 return (event & 0x80) ? 1 : 0;
244 }
245
process_keys(struct lm8323_chip * lm)246 static void process_keys(struct lm8323_chip *lm)
247 {
248 u8 event;
249 u8 key_fifo[LM8323_FIFO_LEN + 1];
250 int old_keys_down = lm->keys_down;
251 int ret;
252 int i = 0;
253
254 /*
255 * Read all key events from the FIFO at once. Next READ_FIFO clears the
256 * FIFO even if we didn't read all events previously.
257 */
258 ret = lm8323_read(lm, LM8323_CMD_READ_FIFO, key_fifo, LM8323_FIFO_LEN);
259
260 if (ret < 0) {
261 dev_err(&lm->client->dev, "Failed reading fifo \n");
262 return;
263 }
264 key_fifo[ret] = 0;
265
266 while ((event = key_fifo[i++])) {
267 u8 key = lm8323_whichkey(event);
268 int isdown = lm8323_ispress(event);
269 unsigned short keycode = lm->keymap[key];
270
271 dev_vdbg(&lm->client->dev, "key 0x%02x %s\n",
272 key, isdown ? "down" : "up");
273
274 if (lm->kp_enabled) {
275 input_event(lm->idev, EV_MSC, MSC_SCAN, key);
276 input_report_key(lm->idev, keycode, isdown);
277 input_sync(lm->idev);
278 }
279
280 if (isdown)
281 lm->keys_down++;
282 else
283 lm->keys_down--;
284 }
285
286 /*
287 * Errata: We need to ensure that the chip never enters halt mode
288 * during a keypress, so set active time to 0. When it's released,
289 * we can enter halt again, so set the active time back to normal.
290 */
291 if (!old_keys_down && lm->keys_down)
292 lm8323_set_active_time(lm, 0);
293 if (old_keys_down && !lm->keys_down)
294 lm8323_set_active_time(lm, lm->active_time);
295 }
296
lm8323_process_error(struct lm8323_chip * lm)297 static void lm8323_process_error(struct lm8323_chip *lm)
298 {
299 u8 error;
300
301 if (lm8323_read(lm, LM8323_CMD_READ_ERR, &error, 1) == 1) {
302 if (error & ERR_FIFOOVER)
303 dev_vdbg(&lm->client->dev, "fifo overflow!\n");
304 if (error & ERR_KEYOVR)
305 dev_vdbg(&lm->client->dev,
306 "more than two keys pressed\n");
307 if (error & ERR_CMDUNK)
308 dev_vdbg(&lm->client->dev,
309 "unknown command submitted\n");
310 if (error & ERR_BADPAR)
311 dev_vdbg(&lm->client->dev, "bad command parameter\n");
312 }
313 }
314
lm8323_reset(struct lm8323_chip * lm)315 static void lm8323_reset(struct lm8323_chip *lm)
316 {
317 /* The docs say we must pass 0xAA as the data byte. */
318 lm8323_write(lm, 2, LM8323_CMD_RESET, 0xAA);
319 }
320
lm8323_configure(struct lm8323_chip * lm)321 static int lm8323_configure(struct lm8323_chip *lm)
322 {
323 int keysize = (lm->size_x << 4) | lm->size_y;
324 int clock = (CLK_SLOWCLKEN | CLK_RCPWM_EXTERNAL);
325 int debounce = lm->debounce_time >> 2;
326 int active = lm->active_time >> 2;
327
328 /*
329 * Active time must be greater than the debounce time: if it's
330 * a close-run thing, give ourselves a 12ms buffer.
331 */
332 if (debounce >= active)
333 active = debounce + 3;
334
335 lm8323_write(lm, 2, LM8323_CMD_WRITE_CFG, 0);
336 lm8323_write(lm, 2, LM8323_CMD_WRITE_CLOCK, clock);
337 lm8323_write(lm, 2, LM8323_CMD_SET_KEY_SIZE, keysize);
338 lm8323_set_active_time(lm, lm->active_time);
339 lm8323_write(lm, 2, LM8323_CMD_SET_DEBOUNCE, debounce);
340 lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_STATE, 0xff, 0xff);
341 lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_SEL, 0, 0);
342
343 /*
344 * Not much we can do about errors at this point, so just hope
345 * for the best.
346 */
347
348 return 0;
349 }
350
pwm_done(struct lm8323_pwm * pwm)351 static void pwm_done(struct lm8323_pwm *pwm)
352 {
353 guard(mutex)(&pwm->lock);
354
355 pwm->running = false;
356 if (pwm->desired_brightness != pwm->brightness)
357 schedule_work(&pwm->work);
358 }
359
360 /*
361 * Bottom half: handle the interrupt by posting key events, or dealing with
362 * errors appropriately.
363 */
lm8323_irq(int irq,void * _lm)364 static irqreturn_t lm8323_irq(int irq, void *_lm)
365 {
366 struct lm8323_chip *lm = _lm;
367 u8 ints;
368 int i;
369
370 guard(mutex)(&lm->lock);
371
372 while ((lm8323_read(lm, LM8323_CMD_READ_INT, &ints, 1) == 1) && ints) {
373 if (likely(ints & INT_KEYPAD))
374 process_keys(lm);
375 if (ints & INT_ROTATOR) {
376 /* We don't currently support the rotator. */
377 dev_vdbg(&lm->client->dev, "rotator fired\n");
378 }
379 if (ints & INT_ERROR) {
380 dev_vdbg(&lm->client->dev, "error!\n");
381 lm8323_process_error(lm);
382 }
383 if (ints & INT_NOINIT) {
384 dev_err(&lm->client->dev, "chip lost config; "
385 "reinitialising\n");
386 lm8323_configure(lm);
387 }
388 for (i = 0; i < LM8323_NUM_PWMS; i++) {
389 if (ints & (INT_PWM1 << i)) {
390 dev_vdbg(&lm->client->dev,
391 "pwm%d engine completed\n", i);
392 pwm_done(&lm->pwm[i]);
393 }
394 }
395 }
396
397 return IRQ_HANDLED;
398 }
399
400 /*
401 * Read the chip ID.
402 */
lm8323_read_id(struct lm8323_chip * lm,u8 * buf)403 static int lm8323_read_id(struct lm8323_chip *lm, u8 *buf)
404 {
405 int bytes;
406
407 bytes = lm8323_read(lm, LM8323_CMD_READ_ID, buf, 2);
408 if (unlikely(bytes != 2))
409 return -EIO;
410
411 return 0;
412 }
413
lm8323_write_pwm_one(struct lm8323_pwm * pwm,int pos,u16 cmd)414 static void lm8323_write_pwm_one(struct lm8323_pwm *pwm, int pos, u16 cmd)
415 {
416 lm8323_write(pwm->chip, 4, LM8323_CMD_PWM_WRITE, (pos << 2) | pwm->id,
417 (cmd & 0xff00) >> 8, cmd & 0x00ff);
418 }
419
420 /*
421 * Write a script into a given PWM engine, concluding with PWM_END.
422 * If 'kill' is nonzero, the engine will be shut down at the end
423 * of the script, producing a zero output. Otherwise the engine
424 * will be kept running at the final PWM level indefinitely.
425 */
lm8323_write_pwm(struct lm8323_pwm * pwm,int kill,int len,const u16 * cmds)426 static void lm8323_write_pwm(struct lm8323_pwm *pwm, int kill,
427 int len, const u16 *cmds)
428 {
429 int i;
430
431 for (i = 0; i < len; i++)
432 lm8323_write_pwm_one(pwm, i, cmds[i]);
433
434 lm8323_write_pwm_one(pwm, i++, PWM_END(kill));
435 lm8323_write(pwm->chip, 2, LM8323_CMD_START_PWM, pwm->id);
436 pwm->running = true;
437 }
438
lm8323_pwm_work(struct work_struct * work)439 static void lm8323_pwm_work(struct work_struct *work)
440 {
441 struct lm8323_pwm *pwm = work_to_pwm(work);
442 int div512, perstep, steps, hz, up, kill;
443 u16 pwm_cmds[3];
444 int num_cmds = 0;
445
446 guard(mutex)(&pwm->lock);
447
448 /*
449 * Do nothing if we're already at the requested level,
450 * or previous setting is not yet complete. In the latter
451 * case we will be called again when the previous PWM script
452 * finishes.
453 */
454 if (pwm->running || pwm->desired_brightness == pwm->brightness)
455 return;
456
457 kill = (pwm->desired_brightness == 0);
458 up = (pwm->desired_brightness > pwm->brightness);
459 steps = abs(pwm->desired_brightness - pwm->brightness);
460
461 /*
462 * Convert time (in ms) into a divisor (512 or 16 on a refclk of
463 * 32768Hz), and number of ticks per step.
464 */
465 if ((pwm->fade_time / steps) > (32768 / 512)) {
466 div512 = 1;
467 hz = 32768 / 512;
468 } else {
469 div512 = 0;
470 hz = 32768 / 16;
471 }
472
473 perstep = (hz * pwm->fade_time) / (steps * 1000);
474
475 if (perstep == 0)
476 perstep = 1;
477 else if (perstep > 63)
478 perstep = 63;
479
480 while (steps) {
481 int s;
482
483 s = min(126, steps);
484 pwm_cmds[num_cmds++] = PWM_RAMP(div512, perstep, s, up);
485 steps -= s;
486 }
487
488 lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
489 pwm->brightness = pwm->desired_brightness;
490 }
491
lm8323_pwm_set_brightness(struct led_classdev * led_cdev,enum led_brightness brightness)492 static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
493 enum led_brightness brightness)
494 {
495 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
496 struct lm8323_chip *lm = pwm->chip;
497
498 scoped_guard(mutex, &pwm->lock) {
499 pwm->desired_brightness = brightness;
500 }
501
502 if (in_interrupt()) {
503 schedule_work(&pwm->work);
504 } else {
505 /*
506 * Schedule PWM work as usual unless we are going into suspend
507 */
508 scoped_guard(mutex, &lm->lock) {
509 if (likely(!lm->pm_suspend))
510 schedule_work(&pwm->work);
511 else
512 lm8323_pwm_work(&pwm->work);
513 }
514 }
515 }
516
lm8323_pwm_show_time(struct device * dev,struct device_attribute * attr,char * buf)517 static ssize_t lm8323_pwm_show_time(struct device *dev,
518 struct device_attribute *attr, char *buf)
519 {
520 struct led_classdev *led_cdev = dev_get_drvdata(dev);
521 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
522
523 return sprintf(buf, "%d\n", pwm->fade_time);
524 }
525
lm8323_pwm_store_time(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)526 static ssize_t lm8323_pwm_store_time(struct device *dev,
527 struct device_attribute *attr, const char *buf, size_t len)
528 {
529 struct led_classdev *led_cdev = dev_get_drvdata(dev);
530 struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
531 int ret, time;
532
533 ret = kstrtoint(buf, 10, &time);
534 /* Numbers only, please. */
535 if (ret)
536 return ret;
537
538 pwm->fade_time = time;
539
540 return strlen(buf);
541 }
542 static DEVICE_ATTR(time, 0644, lm8323_pwm_show_time, lm8323_pwm_store_time);
543
544 static struct attribute *lm8323_pwm_attrs[] = {
545 &dev_attr_time.attr,
546 NULL
547 };
548 ATTRIBUTE_GROUPS(lm8323_pwm);
549
init_pwm(struct lm8323_chip * lm,int id,struct device * dev,const char * name)550 static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
551 const char *name)
552 {
553 struct lm8323_pwm *pwm;
554 int err;
555
556 BUG_ON(id > 3);
557
558 pwm = &lm->pwm[id - 1];
559
560 pwm->id = id;
561 pwm->fade_time = 0;
562 pwm->brightness = 0;
563 pwm->desired_brightness = 0;
564 pwm->running = false;
565 pwm->enabled = false;
566 INIT_WORK(&pwm->work, lm8323_pwm_work);
567 mutex_init(&pwm->lock);
568 pwm->chip = lm;
569
570 if (name) {
571 pwm->cdev.name = name;
572 pwm->cdev.brightness_set = lm8323_pwm_set_brightness;
573 pwm->cdev.groups = lm8323_pwm_groups;
574
575 err = devm_led_classdev_register(dev, &pwm->cdev);
576 if (err) {
577 dev_err(dev, "couldn't register PWM %d: %d\n", id, err);
578 return err;
579 }
580 pwm->enabled = true;
581 }
582
583 return 0;
584 }
585
lm8323_show_disable(struct device * dev,struct device_attribute * attr,char * buf)586 static ssize_t lm8323_show_disable(struct device *dev,
587 struct device_attribute *attr, char *buf)
588 {
589 struct lm8323_chip *lm = dev_get_drvdata(dev);
590
591 return sprintf(buf, "%u\n", !lm->kp_enabled);
592 }
593
lm8323_set_disable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)594 static ssize_t lm8323_set_disable(struct device *dev,
595 struct device_attribute *attr,
596 const char *buf, size_t count)
597 {
598 struct lm8323_chip *lm = dev_get_drvdata(dev);
599 int ret;
600 unsigned int i;
601
602 ret = kstrtouint(buf, 10, &i);
603 if (ret)
604 return ret;
605
606 guard(mutex)(&lm->lock);
607
608 lm->kp_enabled = !i;
609
610 return count;
611 }
612 static DEVICE_ATTR(disable_kp, 0644, lm8323_show_disable, lm8323_set_disable);
613
614 static struct attribute *lm8323_attrs[] = {
615 &dev_attr_disable_kp.attr,
616 NULL,
617 };
618 ATTRIBUTE_GROUPS(lm8323);
619
lm8323_probe(struct i2c_client * client)620 static int lm8323_probe(struct i2c_client *client)
621 {
622 struct lm8323_platform_data *pdata = dev_get_platdata(&client->dev);
623 struct input_dev *idev;
624 struct lm8323_chip *lm;
625 int pwm;
626 int i, err;
627 unsigned long tmo;
628 u8 data[2];
629
630 if (!pdata || !pdata->size_x || !pdata->size_y) {
631 dev_err(&client->dev, "missing platform_data\n");
632 return -EINVAL;
633 }
634
635 if (pdata->size_x > 8) {
636 dev_err(&client->dev, "invalid x size %d specified\n",
637 pdata->size_x);
638 return -EINVAL;
639 }
640
641 if (pdata->size_y > 12) {
642 dev_err(&client->dev, "invalid y size %d specified\n",
643 pdata->size_y);
644 return -EINVAL;
645 }
646
647 lm = devm_kzalloc(&client->dev, sizeof(*lm), GFP_KERNEL);
648 if (!lm)
649 return -ENOMEM;
650
651 idev = devm_input_allocate_device(&client->dev);
652 if (!idev)
653 return -ENOMEM;
654
655 lm->client = client;
656 lm->idev = idev;
657 mutex_init(&lm->lock);
658
659 lm->size_x = pdata->size_x;
660 lm->size_y = pdata->size_y;
661 dev_vdbg(&client->dev, "Keypad size: %d x %d\n",
662 lm->size_x, lm->size_y);
663
664 lm->debounce_time = pdata->debounce_time;
665 lm->active_time = pdata->active_time;
666
667 lm8323_reset(lm);
668
669 /*
670 * Nothing's set up to service the IRQ yet, so just spin for max.
671 * 100ms until we can configure.
672 */
673 tmo = jiffies + msecs_to_jiffies(100);
674 while (lm8323_read(lm, LM8323_CMD_READ_INT, data, 1) == 1) {
675 if (data[0] & INT_NOINIT)
676 break;
677
678 if (time_after(jiffies, tmo)) {
679 dev_err(&client->dev,
680 "timeout waiting for initialisation\n");
681 break;
682 }
683
684 msleep(1);
685 }
686
687 lm8323_configure(lm);
688
689 /* If a true probe check the device */
690 if (lm8323_read_id(lm, data) != 0) {
691 dev_err(&client->dev, "device not found\n");
692 return -ENODEV;
693 }
694
695 for (pwm = 0; pwm < LM8323_NUM_PWMS; pwm++) {
696 err = init_pwm(lm, pwm + 1, &client->dev,
697 pdata->pwm_names[pwm]);
698 if (err)
699 return err;
700 }
701
702 lm->kp_enabled = true;
703
704 idev->name = pdata->name ? : "LM8323 keypad";
705 snprintf(lm->phys, sizeof(lm->phys),
706 "%s/input-kp", dev_name(&client->dev));
707 idev->phys = lm->phys;
708
709 idev->evbit[0] = BIT(EV_KEY) | BIT(EV_MSC);
710 __set_bit(MSC_SCAN, idev->mscbit);
711 for (i = 0; i < LM8323_KEYMAP_SIZE; i++) {
712 __set_bit(pdata->keymap[i], idev->keybit);
713 lm->keymap[i] = pdata->keymap[i];
714 }
715 __clear_bit(KEY_RESERVED, idev->keybit);
716
717 if (pdata->repeat)
718 __set_bit(EV_REP, idev->evbit);
719
720 err = input_register_device(idev);
721 if (err) {
722 dev_dbg(&client->dev, "error registering input device\n");
723 return err;
724 }
725
726 err = devm_request_threaded_irq(&client->dev, client->irq,
727 NULL, lm8323_irq,
728 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
729 "lm8323", lm);
730 if (err) {
731 dev_err(&client->dev, "could not get IRQ %d\n", client->irq);
732 return err;
733 }
734
735 i2c_set_clientdata(client, lm);
736
737 device_init_wakeup(&client->dev, 1);
738 enable_irq_wake(client->irq);
739
740 return 0;
741 }
742
743 /*
744 * We don't need to explicitly suspend the chip, as it already switches off
745 * when there's no activity.
746 */
lm8323_suspend(struct device * dev)747 static int lm8323_suspend(struct device *dev)
748 {
749 struct i2c_client *client = to_i2c_client(dev);
750 struct lm8323_chip *lm = i2c_get_clientdata(client);
751 int i;
752
753 irq_set_irq_wake(client->irq, 0);
754 disable_irq(client->irq);
755
756 scoped_guard(mutex, &lm->lock) {
757 lm->pm_suspend = true;
758 }
759
760 for (i = 0; i < 3; i++)
761 if (lm->pwm[i].enabled)
762 led_classdev_suspend(&lm->pwm[i].cdev);
763
764 return 0;
765 }
766
lm8323_resume(struct device * dev)767 static int lm8323_resume(struct device *dev)
768 {
769 struct i2c_client *client = to_i2c_client(dev);
770 struct lm8323_chip *lm = i2c_get_clientdata(client);
771 int i;
772
773 scoped_guard(mutex, &lm->lock) {
774 lm->pm_suspend = false;
775 }
776
777 for (i = 0; i < 3; i++)
778 if (lm->pwm[i].enabled)
779 led_classdev_resume(&lm->pwm[i].cdev);
780
781 enable_irq(client->irq);
782 irq_set_irq_wake(client->irq, 1);
783
784 return 0;
785 }
786
787 static DEFINE_SIMPLE_DEV_PM_OPS(lm8323_pm_ops, lm8323_suspend, lm8323_resume);
788
789 static const struct i2c_device_id lm8323_id[] = {
790 { "lm8323" },
791 { }
792 };
793
794 static struct i2c_driver lm8323_i2c_driver = {
795 .driver = {
796 .name = "lm8323",
797 .pm = pm_sleep_ptr(&lm8323_pm_ops),
798 .dev_groups = lm8323_groups,
799 },
800 .probe = lm8323_probe,
801 .id_table = lm8323_id,
802 };
803 MODULE_DEVICE_TABLE(i2c, lm8323_id);
804
805 module_i2c_driver(lm8323_i2c_driver);
806
807 MODULE_AUTHOR("Timo O. Karjalainen <timo.o.karjalainen@nokia.com>");
808 MODULE_AUTHOR("Daniel Stone");
809 MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
810 MODULE_DESCRIPTION("LM8323 keypad driver");
811 MODULE_LICENSE("GPL");
812
813