ab3100-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * Copyright (C) 2007-2009 ST-Ericsson
  3. * License terms: GNU General Public License (GPL) version 2
  4. * Low-level core for exclusive access to the AB3100 IC on the I2C bus
  5. * and some basic chip-configuration.
  6. * Author: Linus Walleij <linus.walleij@stericsson.com>
  7. */
  8. #include <linux/i2c.h>
  9. #include <linux/mutex.h>
  10. #include <linux/list.h>
  11. #include <linux/notifier.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/mfd/ab3100.h>
  20. /* These are the only registers inside AB3100 used in this main file */
  21. /* Interrupt event registers */
  22. #define AB3100_EVENTA1 0x21
  23. #define AB3100_EVENTA2 0x22
  24. #define AB3100_EVENTA3 0x23
  25. /* AB3100 DAC converter registers */
  26. #define AB3100_DIS 0x00
  27. #define AB3100_D0C 0x01
  28. #define AB3100_D1C 0x02
  29. #define AB3100_D2C 0x03
  30. #define AB3100_D3C 0x04
  31. /* Chip ID register */
  32. #define AB3100_CID 0x20
  33. /* AB3100 interrupt registers */
  34. #define AB3100_IMRA1 0x24
  35. #define AB3100_IMRA2 0x25
  36. #define AB3100_IMRA3 0x26
  37. #define AB3100_IMRB1 0x2B
  38. #define AB3100_IMRB2 0x2C
  39. #define AB3100_IMRB3 0x2D
  40. /* System Power Monitoring and control registers */
  41. #define AB3100_MCA 0x2E
  42. #define AB3100_MCB 0x2F
  43. /* SIM power up */
  44. #define AB3100_SUP 0x50
  45. /*
  46. * I2C communication
  47. *
  48. * The AB3100 is usually assigned address 0x48 (7-bit)
  49. * The chip is defined in the platform i2c_board_data section.
  50. */
  51. u8 ab3100_get_chip_type(struct ab3100 *ab3100)
  52. {
  53. u8 chip = ABUNKNOWN;
  54. switch (ab3100->chip_id & 0xf0) {
  55. case 0xa0:
  56. chip = AB3000;
  57. break;
  58. case 0xc0:
  59. chip = AB3100;
  60. break;
  61. }
  62. return chip;
  63. }
  64. EXPORT_SYMBOL(ab3100_get_chip_type);
  65. int ab3100_set_register_interruptible(struct ab3100 *ab3100, u8 reg, u8 regval)
  66. {
  67. u8 regandval[2] = {reg, regval};
  68. int err;
  69. err = mutex_lock_interruptible(&ab3100->access_mutex);
  70. if (err)
  71. return err;
  72. /*
  73. * A two-byte write message with the first byte containing the register
  74. * number and the second byte containing the value to be written
  75. * effectively sets a register in the AB3100.
  76. */
  77. err = i2c_master_send(ab3100->i2c_client, regandval, 2);
  78. if (err < 0) {
  79. dev_err(ab3100->dev,
  80. "write error (write register): %d\n",
  81. err);
  82. } else if (err != 2) {
  83. dev_err(ab3100->dev,
  84. "write error (write register) "
  85. "%d bytes transferred (expected 2)\n",
  86. err);
  87. err = -EIO;
  88. } else {
  89. /* All is well */
  90. err = 0;
  91. }
  92. mutex_unlock(&ab3100->access_mutex);
  93. return err;
  94. }
  95. EXPORT_SYMBOL(ab3100_set_register_interruptible);
  96. /*
  97. * The test registers exist at an I2C bus address up one
  98. * from the ordinary base. They are not supposed to be used
  99. * in production code, but sometimes you have to do that
  100. * anyway. It's currently only used from this file so declare
  101. * it static and do not export.
  102. */
  103. static int ab3100_set_test_register_interruptible(struct ab3100 *ab3100,
  104. u8 reg, u8 regval)
  105. {
  106. u8 regandval[2] = {reg, regval};
  107. int err;
  108. err = mutex_lock_interruptible(&ab3100->access_mutex);
  109. if (err)
  110. return err;
  111. err = i2c_master_send(ab3100->testreg_client, regandval, 2);
  112. if (err < 0) {
  113. dev_err(ab3100->dev,
  114. "write error (write test register): %d\n",
  115. err);
  116. } else if (err != 2) {
  117. dev_err(ab3100->dev,
  118. "write error (write test register) "
  119. "%d bytes transferred (expected 2)\n",
  120. err);
  121. err = -EIO;
  122. } else {
  123. /* All is well */
  124. err = 0;
  125. }
  126. mutex_unlock(&ab3100->access_mutex);
  127. return err;
  128. }
  129. int ab3100_get_register_interruptible(struct ab3100 *ab3100, u8 reg, u8 *regval)
  130. {
  131. int err;
  132. err = mutex_lock_interruptible(&ab3100->access_mutex);
  133. if (err)
  134. return err;
  135. /*
  136. * AB3100 require an I2C "stop" command between each message, else
  137. * it will not work. The only way of achieveing this with the
  138. * message transport layer is to send the read and write messages
  139. * separately.
  140. */
  141. err = i2c_master_send(ab3100->i2c_client, &reg, 1);
  142. if (err < 0) {
  143. dev_err(ab3100->dev,
  144. "write error (send register address): %d\n",
  145. err);
  146. goto get_reg_out_unlock;
  147. } else if (err != 1) {
  148. dev_err(ab3100->dev,
  149. "write error (send register address) "
  150. "%d bytes transferred (expected 1)\n",
  151. err);
  152. err = -EIO;
  153. goto get_reg_out_unlock;
  154. } else {
  155. /* All is well */
  156. err = 0;
  157. }
  158. err = i2c_master_recv(ab3100->i2c_client, regval, 1);
  159. if (err < 0) {
  160. dev_err(ab3100->dev,
  161. "write error (read register): %d\n",
  162. err);
  163. goto get_reg_out_unlock;
  164. } else if (err != 1) {
  165. dev_err(ab3100->dev,
  166. "write error (read register) "
  167. "%d bytes transferred (expected 1)\n",
  168. err);
  169. err = -EIO;
  170. goto get_reg_out_unlock;
  171. } else {
  172. /* All is well */
  173. err = 0;
  174. }
  175. get_reg_out_unlock:
  176. mutex_unlock(&ab3100->access_mutex);
  177. return err;
  178. }
  179. EXPORT_SYMBOL(ab3100_get_register_interruptible);
  180. int ab3100_get_register_page_interruptible(struct ab3100 *ab3100,
  181. u8 first_reg, u8 *regvals, u8 numregs)
  182. {
  183. int err;
  184. if (ab3100->chip_id == 0xa0 ||
  185. ab3100->chip_id == 0xa1)
  186. /* These don't support paged reads */
  187. return -EIO;
  188. err = mutex_lock_interruptible(&ab3100->access_mutex);
  189. if (err)
  190. return err;
  191. /*
  192. * Paged read also require an I2C "stop" command.
  193. */
  194. err = i2c_master_send(ab3100->i2c_client, &first_reg, 1);
  195. if (err < 0) {
  196. dev_err(ab3100->dev,
  197. "write error (send first register address): %d\n",
  198. err);
  199. goto get_reg_page_out_unlock;
  200. } else if (err != 1) {
  201. dev_err(ab3100->dev,
  202. "write error (send first register address) "
  203. "%d bytes transferred (expected 1)\n",
  204. err);
  205. err = -EIO;
  206. goto get_reg_page_out_unlock;
  207. }
  208. err = i2c_master_recv(ab3100->i2c_client, regvals, numregs);
  209. if (err < 0) {
  210. dev_err(ab3100->dev,
  211. "write error (read register page): %d\n",
  212. err);
  213. goto get_reg_page_out_unlock;
  214. } else if (err != numregs) {
  215. dev_err(ab3100->dev,
  216. "write error (read register page) "
  217. "%d bytes transferred (expected %d)\n",
  218. err, numregs);
  219. err = -EIO;
  220. goto get_reg_page_out_unlock;
  221. }
  222. /* All is well */
  223. err = 0;
  224. get_reg_page_out_unlock:
  225. mutex_unlock(&ab3100->access_mutex);
  226. return err;
  227. }
  228. EXPORT_SYMBOL(ab3100_get_register_page_interruptible);
  229. int ab3100_mask_and_set_register_interruptible(struct ab3100 *ab3100,
  230. u8 reg, u8 andmask, u8 ormask)
  231. {
  232. u8 regandval[2] = {reg, 0};
  233. int err;
  234. err = mutex_lock_interruptible(&ab3100->access_mutex);
  235. if (err)
  236. return err;
  237. /* First read out the target register */
  238. err = i2c_master_send(ab3100->i2c_client, &reg, 1);
  239. if (err < 0) {
  240. dev_err(ab3100->dev,
  241. "write error (maskset send address): %d\n",
  242. err);
  243. goto get_maskset_unlock;
  244. } else if (err != 1) {
  245. dev_err(ab3100->dev,
  246. "write error (maskset send address) "
  247. "%d bytes transferred (expected 1)\n",
  248. err);
  249. err = -EIO;
  250. goto get_maskset_unlock;
  251. }
  252. err = i2c_master_recv(ab3100->i2c_client, &regandval[1], 1);
  253. if (err < 0) {
  254. dev_err(ab3100->dev,
  255. "write error (maskset read register): %d\n",
  256. err);
  257. goto get_maskset_unlock;
  258. } else if (err != 1) {
  259. dev_err(ab3100->dev,
  260. "write error (maskset read register) "
  261. "%d bytes transferred (expected 1)\n",
  262. err);
  263. err = -EIO;
  264. goto get_maskset_unlock;
  265. }
  266. /* Modify the register */
  267. regandval[1] &= andmask;
  268. regandval[1] |= ormask;
  269. /* Write the register */
  270. err = i2c_master_send(ab3100->i2c_client, regandval, 2);
  271. if (err < 0) {
  272. dev_err(ab3100->dev,
  273. "write error (write register): %d\n",
  274. err);
  275. goto get_maskset_unlock;
  276. } else if (err != 2) {
  277. dev_err(ab3100->dev,
  278. "write error (write register) "
  279. "%d bytes transferred (expected 2)\n",
  280. err);
  281. err = -EIO;
  282. goto get_maskset_unlock;
  283. }
  284. /* All is well */
  285. err = 0;
  286. get_maskset_unlock:
  287. mutex_unlock(&ab3100->access_mutex);
  288. return err;
  289. }
  290. EXPORT_SYMBOL(ab3100_mask_and_set_register_interruptible);
  291. /*
  292. * Register a simple callback for handling any AB3100 events.
  293. */
  294. int ab3100_event_register(struct ab3100 *ab3100,
  295. struct notifier_block *nb)
  296. {
  297. return blocking_notifier_chain_register(&ab3100->event_subscribers,
  298. nb);
  299. }
  300. EXPORT_SYMBOL(ab3100_event_register);
  301. /*
  302. * Remove a previously registered callback.
  303. */
  304. int ab3100_event_unregister(struct ab3100 *ab3100,
  305. struct notifier_block *nb)
  306. {
  307. return blocking_notifier_chain_unregister(&ab3100->event_subscribers,
  308. nb);
  309. }
  310. EXPORT_SYMBOL(ab3100_event_unregister);
  311. int ab3100_event_registers_startup_state_get(struct ab3100 *ab3100,
  312. u32 *fatevent)
  313. {
  314. if (!ab3100->startup_events_read)
  315. return -EAGAIN; /* Try again later */
  316. *fatevent = ab3100->startup_events;
  317. return 0;
  318. }
  319. EXPORT_SYMBOL(ab3100_event_registers_startup_state_get);
  320. /* Interrupt handling worker */
  321. static void ab3100_work(struct work_struct *work)
  322. {
  323. struct ab3100 *ab3100 = container_of(work, struct ab3100, work);
  324. u8 event_regs[3];
  325. u32 fatevent;
  326. int err;
  327. err = ab3100_get_register_page_interruptible(ab3100, AB3100_EVENTA1,
  328. event_regs, 3);
  329. if (err)
  330. goto err_event_wq;
  331. fatevent = (event_regs[0] << 16) |
  332. (event_regs[1] << 8) |
  333. event_regs[2];
  334. if (!ab3100->startup_events_read) {
  335. ab3100->startup_events = fatevent;
  336. ab3100->startup_events_read = true;
  337. }
  338. /*
  339. * The notified parties will have to mask out the events
  340. * they're interested in and react to them. They will be
  341. * notified on all events, then they use the fatevent value
  342. * to determine if they're interested.
  343. */
  344. blocking_notifier_call_chain(&ab3100->event_subscribers,
  345. fatevent, NULL);
  346. dev_dbg(ab3100->dev,
  347. "IRQ Event: 0x%08x\n", fatevent);
  348. /* By now the IRQ should be acked and deasserted so enable it again */
  349. enable_irq(ab3100->i2c_client->irq);
  350. return;
  351. err_event_wq:
  352. dev_dbg(ab3100->dev,
  353. "error in event workqueue\n");
  354. /* Enable the IRQ anyway, what choice do we have? */
  355. enable_irq(ab3100->i2c_client->irq);
  356. return;
  357. }
  358. static irqreturn_t ab3100_irq_handler(int irq, void *data)
  359. {
  360. struct ab3100 *ab3100 = data;
  361. /*
  362. * Disable the IRQ and dispatch a worker to handle the
  363. * event. Since the chip resides on I2C this is slow
  364. * stuff and we will re-enable the interrupts once th
  365. * worker has finished.
  366. */
  367. disable_irq_nosync(irq);
  368. schedule_work(&ab3100->work);
  369. return IRQ_HANDLED;
  370. }
  371. #ifdef CONFIG_DEBUG_FS
  372. /*
  373. * Some debugfs entries only exposed if we're using debug
  374. */
  375. static int ab3100_registers_print(struct seq_file *s, void *p)
  376. {
  377. struct ab3100 *ab3100 = s->private;
  378. u8 value;
  379. u8 reg;
  380. seq_printf(s, "AB3100 registers:\n");
  381. for (reg = 0; reg < 0xff; reg++) {
  382. ab3100_get_register_interruptible(ab3100, reg, &value);
  383. seq_printf(s, "[0x%x]: 0x%x\n", reg, value);
  384. }
  385. return 0;
  386. }
  387. static int ab3100_registers_open(struct inode *inode, struct file *file)
  388. {
  389. return single_open(file, ab3100_registers_print, inode->i_private);
  390. }
  391. static const struct file_operations ab3100_registers_fops = {
  392. .open = ab3100_registers_open,
  393. .read = seq_read,
  394. .llseek = seq_lseek,
  395. .release = single_release,
  396. .owner = THIS_MODULE,
  397. };
  398. struct ab3100_get_set_reg_priv {
  399. struct ab3100 *ab3100;
  400. bool mode;
  401. };
  402. static int ab3100_get_set_reg_open_file(struct inode *inode, struct file *file)
  403. {
  404. file->private_data = inode->i_private;
  405. return 0;
  406. }
  407. static ssize_t ab3100_get_set_reg(struct file *file,
  408. const char __user *user_buf,
  409. size_t count, loff_t *ppos)
  410. {
  411. struct ab3100_get_set_reg_priv *priv = file->private_data;
  412. struct ab3100 *ab3100 = priv->ab3100;
  413. char buf[32];
  414. ssize_t buf_size;
  415. int regp;
  416. unsigned long user_reg;
  417. int err;
  418. int i = 0;
  419. /* Get userspace string and assure termination */
  420. buf_size = min(count, (sizeof(buf)-1));
  421. if (copy_from_user(buf, user_buf, buf_size))
  422. return -EFAULT;
  423. buf[buf_size] = 0;
  424. /*
  425. * The idea is here to parse a string which is either
  426. * "0xnn" for reading a register, or "0xaa 0xbb" for
  427. * writing 0xbb to the register 0xaa. First move past
  428. * whitespace and then begin to parse the register.
  429. */
  430. while ((i < buf_size) && (buf[i] == ' '))
  431. i++;
  432. regp = i;
  433. /*
  434. * Advance pointer to end of string then terminate
  435. * the register string. This is needed to satisfy
  436. * the strict_strtoul() function.
  437. */
  438. while ((i < buf_size) && (buf[i] != ' '))
  439. i++;
  440. buf[i] = '\0';
  441. err = strict_strtoul(&buf[regp], 16, &user_reg);
  442. if (err)
  443. return err;
  444. if (user_reg > 0xff)
  445. return -EINVAL;
  446. /* Either we read or we write a register here */
  447. if (!priv->mode) {
  448. /* Reading */
  449. u8 reg = (u8) user_reg;
  450. u8 regvalue;
  451. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  452. dev_info(ab3100->dev,
  453. "debug read AB3100 reg[0x%02x]: 0x%02x\n",
  454. reg, regvalue);
  455. } else {
  456. int valp;
  457. unsigned long user_value;
  458. u8 reg = (u8) user_reg;
  459. u8 value;
  460. u8 regvalue;
  461. /*
  462. * Writing, we need some value to write to
  463. * the register so keep parsing the string
  464. * from userspace.
  465. */
  466. i++;
  467. while ((i < buf_size) && (buf[i] == ' '))
  468. i++;
  469. valp = i;
  470. while ((i < buf_size) && (buf[i] != ' '))
  471. i++;
  472. buf[i] = '\0';
  473. err = strict_strtoul(&buf[valp], 16, &user_value);
  474. if (err)
  475. return err;
  476. if (user_reg > 0xff)
  477. return -EINVAL;
  478. value = (u8) user_value;
  479. ab3100_set_register_interruptible(ab3100, reg, value);
  480. ab3100_get_register_interruptible(ab3100, reg, &regvalue);
  481. dev_info(ab3100->dev,
  482. "debug write reg[0x%02x] with 0x%02x, "
  483. "after readback: 0x%02x\n",
  484. reg, value, regvalue);
  485. }
  486. return buf_size;
  487. }
  488. static const struct file_operations ab3100_get_set_reg_fops = {
  489. .open = ab3100_get_set_reg_open_file,
  490. .write = ab3100_get_set_reg,
  491. };
  492. static struct dentry *ab3100_dir;
  493. static struct dentry *ab3100_reg_file;
  494. static struct ab3100_get_set_reg_priv ab3100_get_priv;
  495. static struct dentry *ab3100_get_reg_file;
  496. static struct ab3100_get_set_reg_priv ab3100_set_priv;
  497. static struct dentry *ab3100_set_reg_file;
  498. static void ab3100_setup_debugfs(struct ab3100 *ab3100)
  499. {
  500. int err;
  501. ab3100_dir = debugfs_create_dir("ab3100", NULL);
  502. if (!ab3100_dir)
  503. goto exit_no_debugfs;
  504. ab3100_reg_file = debugfs_create_file("registers",
  505. S_IRUGO, ab3100_dir, ab3100,
  506. &ab3100_registers_fops);
  507. if (!ab3100_reg_file) {
  508. err = -ENOMEM;
  509. goto exit_destroy_dir;
  510. }
  511. ab3100_get_priv.ab3100 = ab3100;
  512. ab3100_get_priv.mode = false;
  513. ab3100_get_reg_file = debugfs_create_file("get_reg",
  514. S_IWUGO, ab3100_dir, &ab3100_get_priv,
  515. &ab3100_get_set_reg_fops);
  516. if (!ab3100_get_reg_file) {
  517. err = -ENOMEM;
  518. goto exit_destroy_reg;
  519. }
  520. ab3100_set_priv.ab3100 = ab3100;
  521. ab3100_set_priv.mode = true;
  522. ab3100_set_reg_file = debugfs_create_file("set_reg",
  523. S_IWUGO, ab3100_dir, &ab3100_set_priv,
  524. &ab3100_get_set_reg_fops);
  525. if (!ab3100_set_reg_file) {
  526. err = -ENOMEM;
  527. goto exit_destroy_get_reg;
  528. }
  529. return;
  530. exit_destroy_get_reg:
  531. debugfs_remove(ab3100_get_reg_file);
  532. exit_destroy_reg:
  533. debugfs_remove(ab3100_reg_file);
  534. exit_destroy_dir:
  535. debugfs_remove(ab3100_dir);
  536. exit_no_debugfs:
  537. return;
  538. }
  539. static inline void ab3100_remove_debugfs(void)
  540. {
  541. debugfs_remove(ab3100_set_reg_file);
  542. debugfs_remove(ab3100_get_reg_file);
  543. debugfs_remove(ab3100_reg_file);
  544. debugfs_remove(ab3100_dir);
  545. }
  546. #else
  547. static inline void ab3100_setup_debugfs(struct ab3100 *ab3100)
  548. {
  549. }
  550. static inline void ab3100_remove_debugfs(void)
  551. {
  552. }
  553. #endif
  554. /*
  555. * Basic set-up, datastructure creation/destruction and I2C interface.
  556. * This sets up a default config in the AB3100 chip so that it
  557. * will work as expected.
  558. */
  559. struct ab3100_init_setting {
  560. u8 abreg;
  561. u8 setting;
  562. };
  563. static const struct ab3100_init_setting __initconst
  564. ab3100_init_settings[] = {
  565. {
  566. .abreg = AB3100_MCA,
  567. .setting = 0x01
  568. }, {
  569. .abreg = AB3100_MCB,
  570. .setting = 0x30
  571. }, {
  572. .abreg = AB3100_IMRA1,
  573. .setting = 0x00
  574. }, {
  575. .abreg = AB3100_IMRA2,
  576. .setting = 0xFF
  577. }, {
  578. .abreg = AB3100_IMRA3,
  579. .setting = 0x01
  580. }, {
  581. .abreg = AB3100_IMRB1,
  582. .setting = 0xBF
  583. }, {
  584. .abreg = AB3100_IMRB2,
  585. .setting = 0xFF
  586. }, {
  587. .abreg = AB3100_IMRB3,
  588. .setting = 0xFF
  589. }, {
  590. .abreg = AB3100_SUP,
  591. .setting = 0x00
  592. }, {
  593. .abreg = AB3100_DIS,
  594. .setting = 0xF0
  595. }, {
  596. .abreg = AB3100_D0C,
  597. .setting = 0x00
  598. }, {
  599. .abreg = AB3100_D1C,
  600. .setting = 0x00
  601. }, {
  602. .abreg = AB3100_D2C,
  603. .setting = 0x00
  604. }, {
  605. .abreg = AB3100_D3C,
  606. .setting = 0x00
  607. },
  608. };
  609. static int __init ab3100_setup(struct ab3100 *ab3100)
  610. {
  611. int err = 0;
  612. int i;
  613. for (i = 0; i < ARRAY_SIZE(ab3100_init_settings); i++) {
  614. err = ab3100_set_register_interruptible(ab3100,
  615. ab3100_init_settings[i].abreg,
  616. ab3100_init_settings[i].setting);
  617. if (err)
  618. goto exit_no_setup;
  619. }
  620. /*
  621. * Special trick to make the AB3100 use the 32kHz clock (RTC)
  622. * bit 3 in test register 0x02 is a special, undocumented test
  623. * register bit that only exist in AB3100 P1E
  624. */
  625. if (ab3100->chip_id == 0xc4) {
  626. dev_warn(ab3100->dev,
  627. "AB3100 P1E variant detected, "
  628. "forcing chip to 32KHz\n");
  629. err = ab3100_set_test_register_interruptible(ab3100, 0x02, 0x08);
  630. }
  631. exit_no_setup:
  632. return err;
  633. }
  634. /*
  635. * Here we define all the platform devices that appear
  636. * as children of the AB3100. These are regular platform
  637. * devices with the IORESOURCE_IO .start and .end set
  638. * to correspond to the internal AB3100 register range
  639. * mapping to the corresponding subdevice.
  640. */
  641. #define AB3100_DEVICE(devname, devid) \
  642. static struct platform_device ab3100_##devname##_device = { \
  643. .name = devid, \
  644. .id = -1, \
  645. }
  646. /*
  647. * This lists all the subdevices and corresponding register
  648. * ranges.
  649. */
  650. AB3100_DEVICE(dac, "ab3100-dac");
  651. AB3100_DEVICE(leds, "ab3100-leds");
  652. AB3100_DEVICE(power, "ab3100-power");
  653. AB3100_DEVICE(regulators, "ab3100-regulators");
  654. AB3100_DEVICE(sim, "ab3100-sim");
  655. AB3100_DEVICE(uart, "ab3100-uart");
  656. AB3100_DEVICE(rtc, "ab3100-rtc");
  657. AB3100_DEVICE(charger, "ab3100-charger");
  658. AB3100_DEVICE(boost, "ab3100-boost");
  659. AB3100_DEVICE(adc, "ab3100-adc");
  660. AB3100_DEVICE(fuelgauge, "ab3100-fuelgauge");
  661. AB3100_DEVICE(vibrator, "ab3100-vibrator");
  662. AB3100_DEVICE(otp, "ab3100-otp");
  663. AB3100_DEVICE(codec, "ab3100-codec");
  664. static struct platform_device *
  665. ab3100_platform_devs[] = {
  666. &ab3100_dac_device,
  667. &ab3100_leds_device,
  668. &ab3100_power_device,
  669. &ab3100_regulators_device,
  670. &ab3100_sim_device,
  671. &ab3100_uart_device,
  672. &ab3100_rtc_device,
  673. &ab3100_charger_device,
  674. &ab3100_boost_device,
  675. &ab3100_adc_device,
  676. &ab3100_fuelgauge_device,
  677. &ab3100_vibrator_device,
  678. &ab3100_otp_device,
  679. &ab3100_codec_device,
  680. };
  681. struct ab_family_id {
  682. u8 id;
  683. char *name;
  684. };
  685. static const struct ab_family_id ids[] __initdata = {
  686. /* AB3100 */
  687. {
  688. .id = 0xc0,
  689. .name = "P1A"
  690. }, {
  691. .id = 0xc1,
  692. .name = "P1B"
  693. }, {
  694. .id = 0xc2,
  695. .name = "P1C"
  696. }, {
  697. .id = 0xc3,
  698. .name = "P1D"
  699. }, {
  700. .id = 0xc4,
  701. .name = "P1E"
  702. }, {
  703. .id = 0xc5,
  704. .name = "P1F/R1A"
  705. }, {
  706. .id = 0xc6,
  707. .name = "P1G/R1A"
  708. }, {
  709. .id = 0xc7,
  710. .name = "P2A/R2A"
  711. }, {
  712. .id = 0xc8,
  713. .name = "P2B/R2B"
  714. },
  715. /* AB3000 variants, not supported */
  716. {
  717. .id = 0xa0
  718. }, {
  719. .id = 0xa1
  720. }, {
  721. .id = 0xa2
  722. }, {
  723. .id = 0xa3
  724. }, {
  725. .id = 0xa4
  726. }, {
  727. .id = 0xa5
  728. }, {
  729. .id = 0xa6
  730. }, {
  731. .id = 0xa7
  732. },
  733. /* Terminator */
  734. {
  735. .id = 0x00,
  736. },
  737. };
  738. static int __init ab3100_probe(struct i2c_client *client,
  739. const struct i2c_device_id *id)
  740. {
  741. struct ab3100 *ab3100;
  742. struct ab3100_platform_data *ab3100_plf_data =
  743. client->dev.platform_data;
  744. int err;
  745. int i;
  746. ab3100 = kzalloc(sizeof(struct ab3100), GFP_KERNEL);
  747. if (!ab3100) {
  748. dev_err(&client->dev, "could not allocate AB3100 device\n");
  749. return -ENOMEM;
  750. }
  751. /* Initialize data structure */
  752. mutex_init(&ab3100->access_mutex);
  753. BLOCKING_INIT_NOTIFIER_HEAD(&ab3100->event_subscribers);
  754. ab3100->i2c_client = client;
  755. ab3100->dev = &ab3100->i2c_client->dev;
  756. i2c_set_clientdata(client, ab3100);
  757. /* Read chip ID register */
  758. err = ab3100_get_register_interruptible(ab3100, AB3100_CID,
  759. &ab3100->chip_id);
  760. if (err) {
  761. dev_err(&client->dev,
  762. "could not communicate with the AB3100 analog "
  763. "baseband chip\n");
  764. goto exit_no_detect;
  765. }
  766. for (i = 0; ids[i].id != 0x0; i++) {
  767. if (ids[i].id == ab3100->chip_id) {
  768. if (ids[i].name != NULL) {
  769. snprintf(&ab3100->chip_name[0],
  770. sizeof(ab3100->chip_name) - 1,
  771. "AB3100 %s",
  772. ids[i].name);
  773. break;
  774. } else {
  775. dev_err(&client->dev,
  776. "AB3000 is not supported\n");
  777. goto exit_no_detect;
  778. }
  779. }
  780. }
  781. if (ids[i].id == 0x0) {
  782. dev_err(&client->dev, "unknown analog baseband chip id: 0x%x\n",
  783. ab3100->chip_id);
  784. dev_err(&client->dev, "accepting it anyway. Please update "
  785. "the driver.\n");
  786. goto exit_no_detect;
  787. }
  788. dev_info(&client->dev, "Detected chip: %s\n",
  789. &ab3100->chip_name[0]);
  790. /* Attach a second dummy i2c_client to the test register address */
  791. ab3100->testreg_client = i2c_new_dummy(client->adapter,
  792. client->addr + 1);
  793. if (!ab3100->testreg_client) {
  794. err = -ENOMEM;
  795. goto exit_no_testreg_client;
  796. }
  797. err = ab3100_setup(ab3100);
  798. if (err)
  799. goto exit_no_setup;
  800. INIT_WORK(&ab3100->work, ab3100_work);
  801. /* This real unpredictable IRQ is of course sampled for entropy */
  802. err = request_irq(client->irq, ab3100_irq_handler,
  803. IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
  804. "AB3100 IRQ", ab3100);
  805. if (err)
  806. goto exit_no_irq;
  807. /* Set parent and a pointer back to the container in device data */
  808. for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++) {
  809. ab3100_platform_devs[i]->dev.parent =
  810. &client->dev;
  811. ab3100_platform_devs[i]->dev.platform_data =
  812. ab3100_plf_data;
  813. platform_set_drvdata(ab3100_platform_devs[i], ab3100);
  814. }
  815. /* Register the platform devices */
  816. platform_add_devices(ab3100_platform_devs,
  817. ARRAY_SIZE(ab3100_platform_devs));
  818. ab3100_setup_debugfs(ab3100);
  819. return 0;
  820. exit_no_irq:
  821. exit_no_setup:
  822. i2c_unregister_device(ab3100->testreg_client);
  823. exit_no_testreg_client:
  824. exit_no_detect:
  825. kfree(ab3100);
  826. return err;
  827. }
  828. static int __exit ab3100_remove(struct i2c_client *client)
  829. {
  830. struct ab3100 *ab3100 = i2c_get_clientdata(client);
  831. int i;
  832. /* Unregister subdevices */
  833. for (i = 0; i < ARRAY_SIZE(ab3100_platform_devs); i++)
  834. platform_device_unregister(ab3100_platform_devs[i]);
  835. ab3100_remove_debugfs();
  836. i2c_unregister_device(ab3100->testreg_client);
  837. /*
  838. * At this point, all subscribers should have unregistered
  839. * their notifiers so deactivate IRQ
  840. */
  841. free_irq(client->irq, ab3100);
  842. kfree(ab3100);
  843. return 0;
  844. }
  845. static const struct i2c_device_id ab3100_id[] = {
  846. { "ab3100", 0 },
  847. { }
  848. };
  849. MODULE_DEVICE_TABLE(i2c, ab3100_id);
  850. static struct i2c_driver ab3100_driver = {
  851. .driver = {
  852. .name = "ab3100",
  853. .owner = THIS_MODULE,
  854. },
  855. .id_table = ab3100_id,
  856. .probe = ab3100_probe,
  857. .remove = __exit_p(ab3100_remove),
  858. };
  859. static int __init ab3100_i2c_init(void)
  860. {
  861. return i2c_add_driver(&ab3100_driver);
  862. }
  863. static void __exit ab3100_i2c_exit(void)
  864. {
  865. i2c_del_driver(&ab3100_driver);
  866. }
  867. subsys_initcall(ab3100_i2c_init);
  868. module_exit(ab3100_i2c_exit);
  869. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  870. MODULE_DESCRIPTION("AB3100 core driver");
  871. MODULE_LICENSE("GPL");