// SPDX-License-Identifier: GPL-2.0-only /* * drivers/i2c/chips/lm8323.c * * Copyright (C) 2007-2009 Nokia Corporation * * Written by Daniel Stone <daniel.stone@nokia.com> * Timo O. Karjalainen <timo.o.karjalainen@nokia.com> * * Updated by Felipe Balbi <felipe.balbi@nokia.com>
*/
/* Commands for PWM engine; feed in with PWM_WRITE. */ /* Load ramp counter from duty cycle field (range 0 - 0xff). */ #define PWM_SET(v) (0x4000 | ((v) & 0xff)) /* Go to start of script. */ #define PWM_GOTOSTART 0x0000 /* * Stop engine (generates interrupt). If reset is 1, clear the program * counter, else leave it.
*/ #define PWM_END(reset) (0xc000 | (!!(reset) << 11)) /* * Ramp. If s is 1, divide clock by 512, else divide clock by 16. * Take t clock scales (up to 63) per step, for n steps (up to 126). * If u is set, ramp up, else ramp down.
*/ #define PWM_RAMP(s, t, n, u) ((!!(s) << 14) | ((t) & 0x3f) << 8 | \
((n) & 0x7f) | ((u) ? 0 : 0x80)) /* * Loop (i.e. jump back to pos) for a given number of iterations (up to 63). * If cnt is zero, execute until PWM_END is encountered.
*/ #define PWM_LOOP(cnt, pos) (0xa000 | (((cnt) & 0x3f) << 7) | \
((pos) & 0x3f)) /* * Wait for trigger. Argument is a mask of channels, shifted by the channel * number, e.g. 0xa for channels 3 and 1. Note that channels are numbered * from 1, not 0.
*/ #define PWM_WAIT_TRIG(chans) (0xe000 | (((chans) & 0x7) << 6)) /* Send trigger. Argument is same as PWM_WAIT_TRIG. */ #define PWM_SEND_TRIG(chans) (0xe000 | ((chans) & 0x7))
struct lm8323_pwm { int id; int fade_time; int brightness; int desired_brightness; bool enabled; bool running; /* pwm lock */ struct mutex lock; struct work_struct work; struct led_classdev cdev; struct lm8323_chip *chip;
};
struct lm8323_chip { /* device lock */ struct mutex lock; struct i2c_client *client; struct input_dev *idev; bool kp_enabled; bool pm_suspend; unsigned keys_down; char phys[32]; unsignedshort keymap[LM8323_KEYMAP_SIZE]; int size_x; int size_y; int debounce_time; int active_time; struct lm8323_pwm pwm[LM8323_NUM_PWMS];
};
/* * To write, we just access the chip's address in write mode, and dump the * command and data out on the bus. The command byte and data are taken as * sequential u8s out of varargs, to a maximum of LM8323_MAX_DATA.
*/ staticint lm8323_write(struct lm8323_chip *lm, int len, ...)
{ int ret, i;
va_list ap;
u8 data[LM8323_MAX_DATA];
va_start(ap, len);
if (unlikely(len > LM8323_MAX_DATA)) {
dev_err(&lm->client->dev, "tried to send %d bytes\n", len);
va_end(ap); return 0;
}
for (i = 0; i < len; i++)
data[i] = va_arg(ap, int);
va_end(ap);
/* * If the host is asleep while we send the data, we can get a NACK * back while it wakes up, so try again, once.
*/
ret = i2c_master_send(lm->client, data, len); if (unlikely(ret == -EREMOTEIO))
ret = i2c_master_send(lm->client, data, len); if (unlikely(ret != len))
dev_err(&lm->client->dev, "sent %d bytes of %d total\n",
len, ret);
return ret;
}
/* * To read, we first send the command byte to the chip and end the transaction, * then access the chip in read mode, at which point it will send the data.
*/ staticint lm8323_read(struct lm8323_chip *lm, u8 cmd, u8 *buf, int len)
{ int ret;
/* * If the host is asleep while we send the byte, we can get a NACK * back while it wakes up, so try again, once.
*/
ret = i2c_master_send(lm->client, &cmd, 1); if (unlikely(ret == -EREMOTEIO))
ret = i2c_master_send(lm->client, &cmd, 1); if (unlikely(ret != 1)) {
dev_err(&lm->client->dev, "sending read cmd 0x%02x failed\n",
cmd); return 0;
}
ret = i2c_master_recv(lm->client, buf, len); if (unlikely(ret != len))
dev_err(&lm->client->dev, "wanted %d bytes, got %d\n",
len, ret);
return ret;
}
/* * Set the chip active time (idle time before it enters halt).
*/ staticvoid lm8323_set_active_time(struct lm8323_chip *lm, int time)
{
lm8323_write(lm, 2, LM8323_CMD_SET_ACTIVE, time >> 2);
}
/* * The signals are AT-style: the low 7 bits are the keycode, and the top * bit indicates the state (1 for down, 0 for up).
*/ staticinline u8 lm8323_whichkey(u8 event)
{ return event & 0x7f;
}
staticvoid process_keys(struct lm8323_chip *lm)
{
u8 event;
u8 key_fifo[LM8323_FIFO_LEN + 1]; int old_keys_down = lm->keys_down; int ret; int i = 0;
/* * Read all key events from the FIFO at once. Next READ_FIFO clears the * FIFO even if we didn't read all events previously.
*/
ret = lm8323_read(lm, LM8323_CMD_READ_FIFO, key_fifo, LM8323_FIFO_LEN);
if (isdown)
lm->keys_down++; else
lm->keys_down--;
}
/* * Errata: We need to ensure that the chip never enters halt mode * during a keypress, so set active time to 0. When it's released, * we can enter halt again, so set the active time back to normal.
*/ if (!old_keys_down && lm->keys_down)
lm8323_set_active_time(lm, 0); if (old_keys_down && !lm->keys_down)
lm8323_set_active_time(lm, lm->active_time);
}
if (lm8323_read(lm, LM8323_CMD_READ_ERR, &error, 1) == 1) { if (error & ERR_FIFOOVER)
dev_vdbg(&lm->client->dev, "fifo overflow!\n"); if (error & ERR_KEYOVR)
dev_vdbg(&lm->client->dev, "more than two keys pressed\n"); if (error & ERR_CMDUNK)
dev_vdbg(&lm->client->dev, "unknown command submitted\n"); if (error & ERR_BADPAR)
dev_vdbg(&lm->client->dev, "bad command parameter\n");
}
}
staticvoid lm8323_reset(struct lm8323_chip *lm)
{ /* The docs say we must pass 0xAA as the data byte. */
lm8323_write(lm, 2, LM8323_CMD_RESET, 0xAA);
}
staticint lm8323_configure(struct lm8323_chip *lm)
{ int keysize = (lm->size_x << 4) | lm->size_y; int clock = (CLK_SLOWCLKEN | CLK_RCPWM_EXTERNAL); int debounce = lm->debounce_time >> 2; int active = lm->active_time >> 2;
/* * Active time must be greater than the debounce time: if it's * a close-run thing, give ourselves a 12ms buffer.
*/ if (debounce >= active)
active = debounce + 3;
/* * Write a script into a given PWM engine, concluding with PWM_END. * If 'kill' is nonzero, the engine will be shut down at the end * of the script, producing a zero output. Otherwise the engine * will be kept running at the final PWM level indefinitely.
*/ staticvoid lm8323_write_pwm(struct lm8323_pwm *pwm, int kill, int len, const u16 *cmds)
{ int i;
for (i = 0; i < len; i++)
lm8323_write_pwm_one(pwm, i, cmds[i]);
/* * Do nothing if we're already at the requested level, * or previous setting is not yet complete. In the latter * case we will be called again when the previous PWM script * finishes.
*/ if (pwm->running || pwm->desired_brightness == pwm->brightness) return;
/* * Convert time (in ms) into a divisor (512 or 16 on a refclk of * 32768Hz), and number of ticks per step.
*/ if ((pwm->fade_time / steps) > (32768 / 512)) {
div512 = 1;
hz = 32768 / 512;
} else {
div512 = 0;
hz = 32768 / 16;
}
if (in_interrupt()) {
schedule_work(&pwm->work);
} else { /* * Schedule PWM work as usual unless we are going into suspend
*/
scoped_guard(mutex, &lm->lock) { if (likely(!lm->pm_suspend))
schedule_work(&pwm->work); else
lm8323_pwm_work(&pwm->work);
}
}
}
/* * Nothing's set up to service the IRQ yet, so just spin for max. * 100ms until we can configure.
*/
tmo = jiffies + msecs_to_jiffies(100); while (lm8323_read(lm, LM8323_CMD_READ_INT, data, 1) == 1) { if (data[0] & INT_NOINIT) break;
if (time_after(jiffies, tmo)) {
dev_err(&client->dev, "timeout waiting for initialisation\n"); break;
}
msleep(1);
}
lm8323_configure(lm);
/* If a true probe check the device */ if (lm8323_read_id(lm, data) != 0) {
dev_err(&client->dev, "device not found\n"); return -ENODEV;
}
/* * We don't need to explicitly suspend the chip, as it already switches off * when there's no activity.
*/ staticint lm8323_suspend(struct device *dev)
{ struct i2c_client *client = to_i2c_client(dev); struct lm8323_chip *lm = i2c_get_clientdata(client); int i;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.