lm8323.c 23 KB

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