lm8323.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 work_struct work;
  139. struct input_dev *idev;
  140. bool kp_enabled;
  141. bool pm_suspend;
  142. unsigned keys_down;
  143. char phys[32];
  144. unsigned short keymap[LM8323_KEYMAP_SIZE];
  145. int size_x;
  146. int size_y;
  147. int debounce_time;
  148. int active_time;
  149. struct lm8323_pwm pwm[LM8323_NUM_PWMS];
  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 work_to_lm8323(w) container_of(w, struct lm8323_chip, work)
  154. #define cdev_to_pwm(c) container_of(c, struct lm8323_pwm, cdev)
  155. #define work_to_pwm(w) container_of(w, struct lm8323_pwm, work)
  156. #define LM8323_MAX_DATA 8
  157. /*
  158. * To write, we just access the chip's address in write mode, and dump the
  159. * command and data out on the bus. The command byte and data are taken as
  160. * sequential u8s out of varargs, to a maximum of LM8323_MAX_DATA.
  161. */
  162. static int lm8323_write(struct lm8323_chip *lm, int len, ...)
  163. {
  164. int ret, i;
  165. va_list ap;
  166. u8 data[LM8323_MAX_DATA];
  167. va_start(ap, len);
  168. if (unlikely(len > LM8323_MAX_DATA)) {
  169. dev_err(&lm->client->dev, "tried to send %d bytes\n", len);
  170. va_end(ap);
  171. return 0;
  172. }
  173. for (i = 0; i < len; i++)
  174. data[i] = va_arg(ap, int);
  175. va_end(ap);
  176. /*
  177. * If the host is asleep while we send the data, we can get a NACK
  178. * back while it wakes up, so try again, once.
  179. */
  180. ret = i2c_master_send(lm->client, data, len);
  181. if (unlikely(ret == -EREMOTEIO))
  182. ret = i2c_master_send(lm->client, data, len);
  183. if (unlikely(ret != len))
  184. dev_err(&lm->client->dev, "sent %d bytes of %d total\n",
  185. len, ret);
  186. return ret;
  187. }
  188. /*
  189. * To read, we first send the command byte to the chip and end the transaction,
  190. * then access the chip in read mode, at which point it will send the data.
  191. */
  192. static int lm8323_read(struct lm8323_chip *lm, u8 cmd, u8 *buf, int len)
  193. {
  194. int ret;
  195. /*
  196. * If the host is asleep while we send the byte, we can get a NACK
  197. * back while it wakes up, so try again, once.
  198. */
  199. ret = i2c_master_send(lm->client, &cmd, 1);
  200. if (unlikely(ret == -EREMOTEIO))
  201. ret = i2c_master_send(lm->client, &cmd, 1);
  202. if (unlikely(ret != 1)) {
  203. dev_err(&lm->client->dev, "sending read cmd 0x%02x failed\n",
  204. cmd);
  205. return 0;
  206. }
  207. ret = i2c_master_recv(lm->client, buf, len);
  208. if (unlikely(ret != len))
  209. dev_err(&lm->client->dev, "wanted %d bytes, got %d\n",
  210. len, ret);
  211. return ret;
  212. }
  213. /*
  214. * Set the chip active time (idle time before it enters halt).
  215. */
  216. static void lm8323_set_active_time(struct lm8323_chip *lm, int time)
  217. {
  218. lm8323_write(lm, 2, LM8323_CMD_SET_ACTIVE, time >> 2);
  219. }
  220. /*
  221. * The signals are AT-style: the low 7 bits are the keycode, and the top
  222. * bit indicates the state (1 for down, 0 for up).
  223. */
  224. static inline u8 lm8323_whichkey(u8 event)
  225. {
  226. return event & 0x7f;
  227. }
  228. static inline int lm8323_ispress(u8 event)
  229. {
  230. return (event & 0x80) ? 1 : 0;
  231. }
  232. static void process_keys(struct lm8323_chip *lm)
  233. {
  234. u8 event;
  235. u8 key_fifo[LM8323_FIFO_LEN + 1];
  236. int old_keys_down = lm->keys_down;
  237. int ret;
  238. int i = 0;
  239. /*
  240. * Read all key events from the FIFO at once. Next READ_FIFO clears the
  241. * FIFO even if we didn't read all events previously.
  242. */
  243. ret = lm8323_read(lm, LM8323_CMD_READ_FIFO, key_fifo, LM8323_FIFO_LEN);
  244. if (ret < 0) {
  245. dev_err(&lm->client->dev, "Failed reading fifo \n");
  246. return;
  247. }
  248. key_fifo[ret] = 0;
  249. while ((event = key_fifo[i++])) {
  250. u8 key = lm8323_whichkey(event);
  251. int isdown = lm8323_ispress(event);
  252. unsigned short keycode = lm->keymap[key];
  253. dev_vdbg(&lm->client->dev, "key 0x%02x %s\n",
  254. key, isdown ? "down" : "up");
  255. if (lm->kp_enabled) {
  256. input_event(lm->idev, EV_MSC, MSC_SCAN, key);
  257. input_report_key(lm->idev, keycode, isdown);
  258. input_sync(lm->idev);
  259. }
  260. if (isdown)
  261. lm->keys_down++;
  262. else
  263. lm->keys_down--;
  264. }
  265. /*
  266. * Errata: We need to ensure that the chip never enters halt mode
  267. * during a keypress, so set active time to 0. When it's released,
  268. * we can enter halt again, so set the active time back to normal.
  269. */
  270. if (!old_keys_down && lm->keys_down)
  271. lm8323_set_active_time(lm, 0);
  272. if (old_keys_down && !lm->keys_down)
  273. lm8323_set_active_time(lm, lm->active_time);
  274. }
  275. static void lm8323_process_error(struct lm8323_chip *lm)
  276. {
  277. u8 error;
  278. if (lm8323_read(lm, LM8323_CMD_READ_ERR, &error, 1) == 1) {
  279. if (error & ERR_FIFOOVER)
  280. dev_vdbg(&lm->client->dev, "fifo overflow!\n");
  281. if (error & ERR_KEYOVR)
  282. dev_vdbg(&lm->client->dev,
  283. "more than two keys pressed\n");
  284. if (error & ERR_CMDUNK)
  285. dev_vdbg(&lm->client->dev,
  286. "unknown command submitted\n");
  287. if (error & ERR_BADPAR)
  288. dev_vdbg(&lm->client->dev, "bad command parameter\n");
  289. }
  290. }
  291. static void lm8323_reset(struct lm8323_chip *lm)
  292. {
  293. /* The docs say we must pass 0xAA as the data byte. */
  294. lm8323_write(lm, 2, LM8323_CMD_RESET, 0xAA);
  295. }
  296. static int lm8323_configure(struct lm8323_chip *lm)
  297. {
  298. int keysize = (lm->size_x << 4) | lm->size_y;
  299. int clock = (CLK_SLOWCLKEN | CLK_RCPWM_EXTERNAL);
  300. int debounce = lm->debounce_time >> 2;
  301. int active = lm->active_time >> 2;
  302. /*
  303. * Active time must be greater than the debounce time: if it's
  304. * a close-run thing, give ourselves a 12ms buffer.
  305. */
  306. if (debounce >= active)
  307. active = debounce + 3;
  308. lm8323_write(lm, 2, LM8323_CMD_WRITE_CFG, 0);
  309. lm8323_write(lm, 2, LM8323_CMD_WRITE_CLOCK, clock);
  310. lm8323_write(lm, 2, LM8323_CMD_SET_KEY_SIZE, keysize);
  311. lm8323_set_active_time(lm, lm->active_time);
  312. lm8323_write(lm, 2, LM8323_CMD_SET_DEBOUNCE, debounce);
  313. lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_STATE, 0xff, 0xff);
  314. lm8323_write(lm, 3, LM8323_CMD_WRITE_PORT_SEL, 0, 0);
  315. /*
  316. * Not much we can do about errors at this point, so just hope
  317. * for the best.
  318. */
  319. return 0;
  320. }
  321. static void pwm_done(struct lm8323_pwm *pwm)
  322. {
  323. mutex_lock(&pwm->lock);
  324. pwm->running = false;
  325. if (pwm->desired_brightness != pwm->brightness)
  326. schedule_work(&pwm->work);
  327. mutex_unlock(&pwm->lock);
  328. }
  329. /*
  330. * Bottom half: handle the interrupt by posting key events, or dealing with
  331. * errors appropriately.
  332. */
  333. static void lm8323_work(struct work_struct *work)
  334. {
  335. struct lm8323_chip *lm = work_to_lm8323(work);
  336. u8 ints;
  337. int i;
  338. mutex_lock(&lm->lock);
  339. while ((lm8323_read(lm, LM8323_CMD_READ_INT, &ints, 1) == 1) && ints) {
  340. if (likely(ints & INT_KEYPAD))
  341. process_keys(lm);
  342. if (ints & INT_ROTATOR) {
  343. /* We don't currently support the rotator. */
  344. dev_vdbg(&lm->client->dev, "rotator fired\n");
  345. }
  346. if (ints & INT_ERROR) {
  347. dev_vdbg(&lm->client->dev, "error!\n");
  348. lm8323_process_error(lm);
  349. }
  350. if (ints & INT_NOINIT) {
  351. dev_err(&lm->client->dev, "chip lost config; "
  352. "reinitialising\n");
  353. lm8323_configure(lm);
  354. }
  355. for (i = 0; i < LM8323_NUM_PWMS; i++) {
  356. if (ints & (1 << (INT_PWM1 + i))) {
  357. dev_vdbg(&lm->client->dev,
  358. "pwm%d engine completed\n", i);
  359. pwm_done(&lm->pwm[i]);
  360. }
  361. }
  362. }
  363. mutex_unlock(&lm->lock);
  364. }
  365. /*
  366. * We cannot use I2C in interrupt context, so we just schedule work.
  367. */
  368. static irqreturn_t lm8323_irq(int irq, void *data)
  369. {
  370. struct lm8323_chip *lm = data;
  371. schedule_work(&lm->work);
  372. return IRQ_HANDLED;
  373. }
  374. /*
  375. * Read the chip ID.
  376. */
  377. static int lm8323_read_id(struct lm8323_chip *lm, u8 *buf)
  378. {
  379. int bytes;
  380. bytes = lm8323_read(lm, LM8323_CMD_READ_ID, buf, 2);
  381. if (unlikely(bytes != 2))
  382. return -EIO;
  383. return 0;
  384. }
  385. static void lm8323_write_pwm_one(struct lm8323_pwm *pwm, int pos, u16 cmd)
  386. {
  387. lm8323_write(pwm->chip, 4, LM8323_CMD_PWM_WRITE, (pos << 2) | pwm->id,
  388. (cmd & 0xff00) >> 8, cmd & 0x00ff);
  389. }
  390. /*
  391. * Write a script into a given PWM engine, concluding with PWM_END.
  392. * If 'kill' is nonzero, the engine will be shut down at the end
  393. * of the script, producing a zero output. Otherwise the engine
  394. * will be kept running at the final PWM level indefinitely.
  395. */
  396. static void lm8323_write_pwm(struct lm8323_pwm *pwm, int kill,
  397. int len, const u16 *cmds)
  398. {
  399. int i;
  400. for (i = 0; i < len; i++)
  401. lm8323_write_pwm_one(pwm, i, cmds[i]);
  402. lm8323_write_pwm_one(pwm, i++, PWM_END(kill));
  403. lm8323_write(pwm->chip, 2, LM8323_CMD_START_PWM, pwm->id);
  404. pwm->running = true;
  405. }
  406. static void lm8323_pwm_work(struct work_struct *work)
  407. {
  408. struct lm8323_pwm *pwm = work_to_pwm(work);
  409. int div512, perstep, steps, hz, up, kill;
  410. u16 pwm_cmds[3];
  411. int num_cmds = 0;
  412. mutex_lock(&pwm->lock);
  413. /*
  414. * Do nothing if we're already at the requested level,
  415. * or previous setting is not yet complete. In the latter
  416. * case we will be called again when the previous PWM script
  417. * finishes.
  418. */
  419. if (pwm->running || pwm->desired_brightness == pwm->brightness)
  420. goto out;
  421. kill = (pwm->desired_brightness == 0);
  422. up = (pwm->desired_brightness > pwm->brightness);
  423. steps = abs(pwm->desired_brightness - pwm->brightness);
  424. /*
  425. * Convert time (in ms) into a divisor (512 or 16 on a refclk of
  426. * 32768Hz), and number of ticks per step.
  427. */
  428. if ((pwm->fade_time / steps) > (32768 / 512)) {
  429. div512 = 1;
  430. hz = 32768 / 512;
  431. } else {
  432. div512 = 0;
  433. hz = 32768 / 16;
  434. }
  435. perstep = (hz * pwm->fade_time) / (steps * 1000);
  436. if (perstep == 0)
  437. perstep = 1;
  438. else if (perstep > 63)
  439. perstep = 63;
  440. while (steps) {
  441. int s;
  442. s = min(126, steps);
  443. pwm_cmds[num_cmds++] = PWM_RAMP(div512, perstep, s, up);
  444. steps -= s;
  445. }
  446. lm8323_write_pwm(pwm, kill, num_cmds, pwm_cmds);
  447. pwm->brightness = pwm->desired_brightness;
  448. out:
  449. mutex_unlock(&pwm->lock);
  450. }
  451. static void lm8323_pwm_set_brightness(struct led_classdev *led_cdev,
  452. enum led_brightness brightness)
  453. {
  454. struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
  455. struct lm8323_chip *lm = pwm->chip;
  456. mutex_lock(&pwm->lock);
  457. pwm->desired_brightness = brightness;
  458. mutex_unlock(&pwm->lock);
  459. if (in_interrupt()) {
  460. schedule_work(&pwm->work);
  461. } else {
  462. /*
  463. * Schedule PWM work as usual unless we are going into suspend
  464. */
  465. mutex_lock(&lm->lock);
  466. if (likely(!lm->pm_suspend))
  467. schedule_work(&pwm->work);
  468. else
  469. lm8323_pwm_work(&pwm->work);
  470. mutex_unlock(&lm->lock);
  471. }
  472. }
  473. static ssize_t lm8323_pwm_show_time(struct device *dev,
  474. struct device_attribute *attr, char *buf)
  475. {
  476. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  477. struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
  478. return sprintf(buf, "%d\n", pwm->fade_time);
  479. }
  480. static ssize_t lm8323_pwm_store_time(struct device *dev,
  481. struct device_attribute *attr, const char *buf, size_t len)
  482. {
  483. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  484. struct lm8323_pwm *pwm = cdev_to_pwm(led_cdev);
  485. int ret;
  486. unsigned long time;
  487. ret = strict_strtoul(buf, 10, &time);
  488. /* Numbers only, please. */
  489. if (ret)
  490. return -EINVAL;
  491. pwm->fade_time = time;
  492. return strlen(buf);
  493. }
  494. static DEVICE_ATTR(time, 0644, lm8323_pwm_show_time, lm8323_pwm_store_time);
  495. static int init_pwm(struct lm8323_chip *lm, int id, struct device *dev,
  496. const char *name)
  497. {
  498. struct lm8323_pwm *pwm;
  499. BUG_ON(id > 3);
  500. pwm = &lm->pwm[id - 1];
  501. pwm->id = id;
  502. pwm->fade_time = 0;
  503. pwm->brightness = 0;
  504. pwm->desired_brightness = 0;
  505. pwm->running = false;
  506. pwm->enabled = false;
  507. INIT_WORK(&pwm->work, lm8323_pwm_work);
  508. mutex_init(&pwm->lock);
  509. pwm->chip = lm;
  510. if (name) {
  511. pwm->cdev.name = name;
  512. pwm->cdev.brightness_set = lm8323_pwm_set_brightness;
  513. if (led_classdev_register(dev, &pwm->cdev) < 0) {
  514. dev_err(dev, "couldn't register PWM %d\n", id);
  515. return -1;
  516. }
  517. if (device_create_file(pwm->cdev.dev,
  518. &dev_attr_time) < 0) {
  519. dev_err(dev, "couldn't register time attribute\n");
  520. led_classdev_unregister(&pwm->cdev);
  521. return -1;
  522. }
  523. pwm->enabled = true;
  524. }
  525. return 0;
  526. }
  527. static struct i2c_driver lm8323_i2c_driver;
  528. static ssize_t lm8323_show_disable(struct device *dev,
  529. struct device_attribute *attr, char *buf)
  530. {
  531. struct lm8323_chip *lm = dev_get_drvdata(dev);
  532. return sprintf(buf, "%u\n", !lm->kp_enabled);
  533. }
  534. static ssize_t lm8323_set_disable(struct device *dev,
  535. struct device_attribute *attr,
  536. const char *buf, size_t count)
  537. {
  538. struct lm8323_chip *lm = dev_get_drvdata(dev);
  539. int ret;
  540. unsigned long i;
  541. ret = strict_strtoul(buf, 10, &i);
  542. mutex_lock(&lm->lock);
  543. lm->kp_enabled = !i;
  544. mutex_unlock(&lm->lock);
  545. return count;
  546. }
  547. static DEVICE_ATTR(disable_kp, 0644, lm8323_show_disable, lm8323_set_disable);
  548. static int __devinit lm8323_probe(struct i2c_client *client,
  549. const struct i2c_device_id *id)
  550. {
  551. struct lm8323_platform_data *pdata = client->dev.platform_data;
  552. struct input_dev *idev;
  553. struct lm8323_chip *lm;
  554. int pwm;
  555. int i, err;
  556. unsigned long tmo;
  557. u8 data[2];
  558. if (!pdata || !pdata->size_x || !pdata->size_y) {
  559. dev_err(&client->dev, "missing platform_data\n");
  560. return -EINVAL;
  561. }
  562. if (pdata->size_x > 8) {
  563. dev_err(&client->dev, "invalid x size %d specified\n",
  564. pdata->size_x);
  565. return -EINVAL;
  566. }
  567. if (pdata->size_y > 12) {
  568. dev_err(&client->dev, "invalid y size %d specified\n",
  569. pdata->size_y);
  570. return -EINVAL;
  571. }
  572. lm = kzalloc(sizeof *lm, GFP_KERNEL);
  573. idev = input_allocate_device();
  574. if (!lm || !idev) {
  575. err = -ENOMEM;
  576. goto fail1;
  577. }
  578. lm->client = client;
  579. lm->idev = idev;
  580. mutex_init(&lm->lock);
  581. INIT_WORK(&lm->work, lm8323_work);
  582. lm->size_x = pdata->size_x;
  583. lm->size_y = pdata->size_y;
  584. dev_vdbg(&client->dev, "Keypad size: %d x %d\n",
  585. lm->size_x, lm->size_y);
  586. lm->debounce_time = pdata->debounce_time;
  587. lm->active_time = pdata->active_time;
  588. lm8323_reset(lm);
  589. /* Nothing's set up to service the IRQ yet, so just spin for max.
  590. * 100ms until we can configure. */
  591. tmo = jiffies + msecs_to_jiffies(100);
  592. while (lm8323_read(lm, LM8323_CMD_READ_INT, data, 1) == 1) {
  593. if (data[0] & INT_NOINIT)
  594. break;
  595. if (time_after(jiffies, tmo)) {
  596. dev_err(&client->dev,
  597. "timeout waiting for initialisation\n");
  598. break;
  599. }
  600. msleep(1);
  601. }
  602. lm8323_configure(lm);
  603. /* If a true probe check the device */
  604. if (lm8323_read_id(lm, data) != 0) {
  605. dev_err(&client->dev, "device not found\n");
  606. err = -ENODEV;
  607. goto fail1;
  608. }
  609. for (pwm = 0; pwm < LM8323_NUM_PWMS; pwm++) {
  610. err = init_pwm(lm, pwm + 1, &client->dev,
  611. pdata->pwm_names[pwm]);
  612. if (err < 0)
  613. goto fail2;
  614. }
  615. lm->kp_enabled = true;
  616. err = device_create_file(&client->dev, &dev_attr_disable_kp);
  617. if (err < 0)
  618. goto fail2;
  619. idev->name = pdata->name ? : "LM8323 keypad";
  620. snprintf(lm->phys, sizeof(lm->phys),
  621. "%s/input-kp", dev_name(&client->dev));
  622. idev->phys = lm->phys;
  623. idev->evbit[0] = BIT(EV_KEY) | BIT(EV_MSC);
  624. __set_bit(MSC_SCAN, idev->mscbit);
  625. for (i = 0; i < LM8323_KEYMAP_SIZE; i++) {
  626. __set_bit(pdata->keymap[i], idev->keybit);
  627. lm->keymap[i] = pdata->keymap[i];
  628. }
  629. __clear_bit(KEY_RESERVED, idev->keybit);
  630. if (pdata->repeat)
  631. __set_bit(EV_REP, idev->evbit);
  632. err = input_register_device(idev);
  633. if (err) {
  634. dev_dbg(&client->dev, "error registering input device\n");
  635. goto fail3;
  636. }
  637. err = request_irq(client->irq, lm8323_irq,
  638. IRQF_TRIGGER_FALLING | IRQF_DISABLED,
  639. "lm8323", lm);
  640. if (err) {
  641. dev_err(&client->dev, "could not get IRQ %d\n", client->irq);
  642. goto fail4;
  643. }
  644. i2c_set_clientdata(client, lm);
  645. device_init_wakeup(&client->dev, 1);
  646. enable_irq_wake(client->irq);
  647. return 0;
  648. fail4:
  649. input_unregister_device(idev);
  650. idev = NULL;
  651. fail3:
  652. device_remove_file(&client->dev, &dev_attr_disable_kp);
  653. fail2:
  654. while (--pwm >= 0)
  655. if (lm->pwm[pwm].enabled)
  656. led_classdev_unregister(&lm->pwm[pwm].cdev);
  657. fail1:
  658. input_free_device(idev);
  659. kfree(lm);
  660. return err;
  661. }
  662. static int __devexit lm8323_remove(struct i2c_client *client)
  663. {
  664. struct lm8323_chip *lm = i2c_get_clientdata(client);
  665. int i;
  666. disable_irq_wake(client->irq);
  667. free_irq(client->irq, lm);
  668. cancel_work_sync(&lm->work);
  669. input_unregister_device(lm->idev);
  670. device_remove_file(&lm->client->dev, &dev_attr_disable_kp);
  671. for (i = 0; i < 3; i++)
  672. if (lm->pwm[i].enabled)
  673. led_classdev_unregister(&lm->pwm[i].cdev);
  674. kfree(lm);
  675. return 0;
  676. }
  677. #ifdef CONFIG_PM
  678. /*
  679. * We don't need to explicitly suspend the chip, as it already switches off
  680. * when there's no activity.
  681. */
  682. static int lm8323_suspend(struct device *dev)
  683. {
  684. struct i2c_client *client = to_i2c_client(dev);
  685. struct lm8323_chip *lm = i2c_get_clientdata(client);
  686. int i;
  687. set_irq_wake(client->irq, 0);
  688. disable_irq(client->irq);
  689. mutex_lock(&lm->lock);
  690. lm->pm_suspend = true;
  691. mutex_unlock(&lm->lock);
  692. for (i = 0; i < 3; i++)
  693. if (lm->pwm[i].enabled)
  694. led_classdev_suspend(&lm->pwm[i].cdev);
  695. return 0;
  696. }
  697. static int lm8323_resume(struct device *dev)
  698. {
  699. struct i2c_client *client = to_i2c_client(dev);
  700. struct lm8323_chip *lm = i2c_get_clientdata(client);
  701. int i;
  702. mutex_lock(&lm->lock);
  703. lm->pm_suspend = false;
  704. mutex_unlock(&lm->lock);
  705. for (i = 0; i < 3; i++)
  706. if (lm->pwm[i].enabled)
  707. led_classdev_resume(&lm->pwm[i].cdev);
  708. enable_irq(client->irq);
  709. set_irq_wake(client->irq, 1);
  710. return 0;
  711. }
  712. #endif
  713. static SIMPLE_DEV_PM_OPS(lm8323_pm_ops, lm8323_suspend, lm8323_resume);
  714. static const struct i2c_device_id lm8323_id[] = {
  715. { "lm8323", 0 },
  716. { }
  717. };
  718. static struct i2c_driver lm8323_i2c_driver = {
  719. .driver = {
  720. .name = "lm8323",
  721. .pm = &lm8323_pm_ops,
  722. },
  723. .probe = lm8323_probe,
  724. .remove = __devexit_p(lm8323_remove),
  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");