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