ab3100-core.c 22 KB

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