lm8323.c 22 KB

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