lm8323.c 21 KB

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