Linux Audio

Check our new training course

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