twl4030-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
  3. *
  4. * Copyright (C) 2005-2006 Texas Instruments, Inc.
  5. *
  6. * Modifications to defer interrupt handling to a kernel thread:
  7. * Copyright (C) 2006 MontaVista Software, Inc.
  8. *
  9. * Based on tlv320aic23.c:
  10. * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
  11. *
  12. * Code cleanup and modifications to IRQ handler.
  13. * by syed khasim <x0khasim@ti.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/init.h>
  30. #include <linux/mutex.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/clk.h>
  33. #include <linux/err.h>
  34. #include <linux/i2c.h>
  35. #include <linux/i2c/twl4030.h>
  36. /*
  37. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  38. * Management and System Companion Device" chips originally designed for
  39. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  40. * often at around 3 Mbit/sec, including for interrupt handling.
  41. *
  42. * This driver core provides genirq support for the interrupts emitted,
  43. * by the various modules, and exports register access primitives.
  44. *
  45. * FIXME this driver currently requires use of the first interrupt line
  46. * (and associated registers).
  47. */
  48. #define DRIVER_NAME "twl4030"
  49. #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
  50. defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
  51. #define twl_has_bci() true
  52. #else
  53. #define twl_has_bci() false
  54. #endif
  55. #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
  56. #define twl_has_keypad() true
  57. #else
  58. #define twl_has_keypad() false
  59. #endif
  60. #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
  61. #define twl_has_gpio() true
  62. #else
  63. #define twl_has_gpio() false
  64. #endif
  65. #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
  66. #define twl_has_madc() true
  67. #else
  68. #define twl_has_madc() false
  69. #endif
  70. #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
  71. #define twl_has_rtc() true
  72. #else
  73. #define twl_has_rtc() false
  74. #endif
  75. #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
  76. #define twl_has_usb() true
  77. #else
  78. #define twl_has_usb() false
  79. #endif
  80. /* Triton Core internal information (BEGIN) */
  81. /* Last - for index max*/
  82. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  83. #define TWL4030_NUM_SLAVES 4
  84. /* Base Address defns for twl4030_map[] */
  85. /* subchip/slave 0 - USB ID */
  86. #define TWL4030_BASEADD_USB 0x0000
  87. /* subchip/slave 1 - AUD ID */
  88. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  89. #define TWL4030_BASEADD_GPIO 0x0098
  90. #define TWL4030_BASEADD_INTBR 0x0085
  91. #define TWL4030_BASEADD_PIH 0x0080
  92. #define TWL4030_BASEADD_TEST 0x004C
  93. /* subchip/slave 2 - AUX ID */
  94. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  95. #define TWL4030_BASEADD_LED 0x00EE
  96. #define TWL4030_BASEADD_MADC 0x0000
  97. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  98. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  99. #define TWL4030_BASEADD_PWM0 0x00F8
  100. #define TWL4030_BASEADD_PWM1 0x00FB
  101. #define TWL4030_BASEADD_PWMA 0x00EF
  102. #define TWL4030_BASEADD_PWMB 0x00F1
  103. #define TWL4030_BASEADD_KEYPAD 0x00D2
  104. /* subchip/slave 3 - POWER ID */
  105. #define TWL4030_BASEADD_BACKUP 0x0014
  106. #define TWL4030_BASEADD_INT 0x002E
  107. #define TWL4030_BASEADD_PM_MASTER 0x0036
  108. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  109. #define TWL4030_BASEADD_RTC 0x001C
  110. #define TWL4030_BASEADD_SECURED_REG 0x0000
  111. /* Triton Core internal information (END) */
  112. /* Few power values */
  113. #define R_CFG_BOOT 0x05
  114. #define R_PROTECT_KEY 0x0E
  115. /* access control values for R_PROTECT_KEY */
  116. #define KEY_UNLOCK1 0xce
  117. #define KEY_UNLOCK2 0xec
  118. #define KEY_LOCK 0x00
  119. /* some fields in R_CFG_BOOT */
  120. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  121. #define HFCLK_FREQ_26_MHZ (2 << 0)
  122. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  123. #define HIGH_PERF_SQ (1 << 3)
  124. /*----------------------------------------------------------------------*/
  125. /* is driver active, bound to a chip? */
  126. static bool inuse;
  127. /* Structure for each TWL4030 Slave */
  128. struct twl4030_client {
  129. struct i2c_client *client;
  130. u8 address;
  131. /* max numb of i2c_msg required is for read =2 */
  132. struct i2c_msg xfer_msg[2];
  133. /* To lock access to xfer_msg */
  134. struct mutex xfer_lock;
  135. };
  136. static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
  137. /* mapping the module id to slave id and base address */
  138. struct twl4030mapping {
  139. unsigned char sid; /* Slave ID */
  140. unsigned char base; /* base address */
  141. };
  142. static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  143. /*
  144. * NOTE: don't change this table without updating the
  145. * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
  146. * so they continue to match the order in this table.
  147. */
  148. { 0, TWL4030_BASEADD_USB },
  149. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  150. { 1, TWL4030_BASEADD_GPIO },
  151. { 1, TWL4030_BASEADD_INTBR },
  152. { 1, TWL4030_BASEADD_PIH },
  153. { 1, TWL4030_BASEADD_TEST },
  154. { 2, TWL4030_BASEADD_KEYPAD },
  155. { 2, TWL4030_BASEADD_MADC },
  156. { 2, TWL4030_BASEADD_INTERRUPTS },
  157. { 2, TWL4030_BASEADD_LED },
  158. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  159. { 2, TWL4030_BASEADD_PRECHARGE },
  160. { 2, TWL4030_BASEADD_PWM0 },
  161. { 2, TWL4030_BASEADD_PWM1 },
  162. { 2, TWL4030_BASEADD_PWMA },
  163. { 2, TWL4030_BASEADD_PWMB },
  164. { 3, TWL4030_BASEADD_BACKUP },
  165. { 3, TWL4030_BASEADD_INT },
  166. { 3, TWL4030_BASEADD_PM_MASTER },
  167. { 3, TWL4030_BASEADD_PM_RECEIVER },
  168. { 3, TWL4030_BASEADD_RTC },
  169. { 3, TWL4030_BASEADD_SECURED_REG },
  170. };
  171. /*----------------------------------------------------------------------*/
  172. /* Exported Functions */
  173. /**
  174. * twl4030_i2c_write - Writes a n bit register in TWL4030
  175. * @mod_no: module number
  176. * @value: an array of num_bytes+1 containing data to write
  177. * @reg: register address (just offset will do)
  178. * @num_bytes: number of bytes to transfer
  179. *
  180. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  181. * valid data starts at Offset 1.
  182. *
  183. * Returns the result of operation - 0 is success
  184. */
  185. int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
  186. {
  187. int ret;
  188. int sid;
  189. struct twl4030_client *twl;
  190. struct i2c_msg *msg;
  191. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  192. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  193. return -EPERM;
  194. }
  195. sid = twl4030_map[mod_no].sid;
  196. twl = &twl4030_modules[sid];
  197. if (unlikely(!inuse)) {
  198. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  199. return -EPERM;
  200. }
  201. mutex_lock(&twl->xfer_lock);
  202. /*
  203. * [MSG1]: fill the register address data
  204. * fill the data Tx buffer
  205. */
  206. msg = &twl->xfer_msg[0];
  207. msg->addr = twl->address;
  208. msg->len = num_bytes + 1;
  209. msg->flags = 0;
  210. msg->buf = value;
  211. /* over write the first byte of buffer with the register address */
  212. *value = twl4030_map[mod_no].base + reg;
  213. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  214. mutex_unlock(&twl->xfer_lock);
  215. /* i2cTransfer returns num messages.translate it pls.. */
  216. if (ret >= 0)
  217. ret = 0;
  218. return ret;
  219. }
  220. EXPORT_SYMBOL(twl4030_i2c_write);
  221. /**
  222. * twl4030_i2c_read - Reads a n bit register in TWL4030
  223. * @mod_no: module number
  224. * @value: an array of num_bytes containing data to be read
  225. * @reg: register address (just offset will do)
  226. * @num_bytes: number of bytes to transfer
  227. *
  228. * Returns result of operation - num_bytes is success else failure.
  229. */
  230. int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, u8 num_bytes)
  231. {
  232. int ret;
  233. u8 val;
  234. int sid;
  235. struct twl4030_client *twl;
  236. struct i2c_msg *msg;
  237. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  238. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  239. return -EPERM;
  240. }
  241. sid = twl4030_map[mod_no].sid;
  242. twl = &twl4030_modules[sid];
  243. if (unlikely(!inuse)) {
  244. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  245. return -EPERM;
  246. }
  247. mutex_lock(&twl->xfer_lock);
  248. /* [MSG1] fill the register address data */
  249. msg = &twl->xfer_msg[0];
  250. msg->addr = twl->address;
  251. msg->len = 1;
  252. msg->flags = 0; /* Read the register value */
  253. val = twl4030_map[mod_no].base + reg;
  254. msg->buf = &val;
  255. /* [MSG2] fill the data rx buffer */
  256. msg = &twl->xfer_msg[1];
  257. msg->addr = twl->address;
  258. msg->flags = I2C_M_RD; /* Read the register value */
  259. msg->len = num_bytes; /* only n bytes */
  260. msg->buf = value;
  261. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  262. mutex_unlock(&twl->xfer_lock);
  263. /* i2cTransfer returns num messages.translate it pls.. */
  264. if (ret >= 0)
  265. ret = 0;
  266. return ret;
  267. }
  268. EXPORT_SYMBOL(twl4030_i2c_read);
  269. /**
  270. * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
  271. * @mod_no: module number
  272. * @value: the value to be written 8 bit
  273. * @reg: register address (just offset will do)
  274. *
  275. * Returns result of operation - 0 is success
  276. */
  277. int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  278. {
  279. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  280. u8 temp_buffer[2] = { 0 };
  281. /* offset 1 contains the data */
  282. temp_buffer[1] = value;
  283. return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
  284. }
  285. EXPORT_SYMBOL(twl4030_i2c_write_u8);
  286. /**
  287. * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
  288. * @mod_no: module number
  289. * @value: the value read 8 bit
  290. * @reg: register address (just offset will do)
  291. *
  292. * Returns result of operation - 0 is success
  293. */
  294. int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  295. {
  296. return twl4030_i2c_read(mod_no, value, reg, 1);
  297. }
  298. EXPORT_SYMBOL(twl4030_i2c_read_u8);
  299. /*----------------------------------------------------------------------*/
  300. /*
  301. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  302. * for the PIH, and the next are for the PWR_INT SIH, since
  303. * that's how twl_init_irq() sets things up.
  304. */
  305. static int add_children(struct twl4030_platform_data *pdata)
  306. {
  307. struct platform_device *pdev = NULL;
  308. struct twl4030_client *twl = NULL;
  309. int status = 0;
  310. if (twl_has_bci() && pdata->bci) {
  311. twl = &twl4030_modules[3];
  312. pdev = platform_device_alloc("twl4030_bci", -1);
  313. if (!pdev) {
  314. pr_debug("%s: can't alloc bci dev\n", DRIVER_NAME);
  315. status = -ENOMEM;
  316. goto err;
  317. }
  318. if (status == 0) {
  319. pdev->dev.parent = &twl->client->dev;
  320. status = platform_device_add_data(pdev, pdata->bci,
  321. sizeof(*pdata->bci));
  322. if (status < 0) {
  323. dev_dbg(&twl->client->dev,
  324. "can't add bci data, %d\n",
  325. status);
  326. goto err;
  327. }
  328. }
  329. if (status == 0) {
  330. struct resource r = {
  331. .start = pdata->irq_base + 8 + 1,
  332. .flags = IORESOURCE_IRQ,
  333. };
  334. status = platform_device_add_resources(pdev, &r, 1);
  335. }
  336. if (status == 0)
  337. status = platform_device_add(pdev);
  338. if (status < 0) {
  339. platform_device_put(pdev);
  340. dev_dbg(&twl->client->dev,
  341. "can't create bci dev, %d\n",
  342. status);
  343. goto err;
  344. }
  345. }
  346. if (twl_has_gpio() && pdata->gpio) {
  347. twl = &twl4030_modules[1];
  348. pdev = platform_device_alloc("twl4030_gpio", -1);
  349. if (!pdev) {
  350. pr_debug("%s: can't alloc gpio dev\n", DRIVER_NAME);
  351. status = -ENOMEM;
  352. goto err;
  353. }
  354. /* more driver model init */
  355. if (status == 0) {
  356. pdev->dev.parent = &twl->client->dev;
  357. /* device_init_wakeup(&pdev->dev, 1); */
  358. status = platform_device_add_data(pdev, pdata->gpio,
  359. sizeof(*pdata->gpio));
  360. if (status < 0) {
  361. dev_dbg(&twl->client->dev,
  362. "can't add gpio data, %d\n",
  363. status);
  364. goto err;
  365. }
  366. }
  367. /* GPIO module IRQ */
  368. if (status == 0) {
  369. struct resource r = {
  370. .start = pdata->irq_base + 0,
  371. .flags = IORESOURCE_IRQ,
  372. };
  373. status = platform_device_add_resources(pdev, &r, 1);
  374. }
  375. if (status == 0)
  376. status = platform_device_add(pdev);
  377. if (status < 0) {
  378. platform_device_put(pdev);
  379. dev_dbg(&twl->client->dev,
  380. "can't create gpio dev, %d\n",
  381. status);
  382. goto err;
  383. }
  384. }
  385. if (twl_has_keypad() && pdata->keypad) {
  386. pdev = platform_device_alloc("twl4030_keypad", -1);
  387. if (pdev) {
  388. twl = &twl4030_modules[2];
  389. pdev->dev.parent = &twl->client->dev;
  390. device_init_wakeup(&pdev->dev, 1);
  391. status = platform_device_add_data(pdev, pdata->keypad,
  392. sizeof(*pdata->keypad));
  393. if (status < 0) {
  394. dev_dbg(&twl->client->dev,
  395. "can't add keypad data, %d\n",
  396. status);
  397. platform_device_put(pdev);
  398. goto err;
  399. }
  400. status = platform_device_add(pdev);
  401. if (status < 0) {
  402. platform_device_put(pdev);
  403. dev_dbg(&twl->client->dev,
  404. "can't create keypad dev, %d\n",
  405. status);
  406. goto err;
  407. }
  408. } else {
  409. pr_debug("%s: can't alloc keypad dev\n", DRIVER_NAME);
  410. status = -ENOMEM;
  411. goto err;
  412. }
  413. }
  414. if (twl_has_madc() && pdata->madc) {
  415. pdev = platform_device_alloc("twl4030_madc", -1);
  416. if (pdev) {
  417. twl = &twl4030_modules[2];
  418. pdev->dev.parent = &twl->client->dev;
  419. device_init_wakeup(&pdev->dev, 1);
  420. status = platform_device_add_data(pdev, pdata->madc,
  421. sizeof(*pdata->madc));
  422. if (status < 0) {
  423. platform_device_put(pdev);
  424. dev_dbg(&twl->client->dev,
  425. "can't add madc data, %d\n",
  426. status);
  427. goto err;
  428. }
  429. status = platform_device_add(pdev);
  430. if (status < 0) {
  431. platform_device_put(pdev);
  432. dev_dbg(&twl->client->dev,
  433. "can't create madc dev, %d\n",
  434. status);
  435. goto err;
  436. }
  437. } else {
  438. pr_debug("%s: can't alloc madc dev\n", DRIVER_NAME);
  439. status = -ENOMEM;
  440. goto err;
  441. }
  442. }
  443. if (twl_has_rtc()) {
  444. twl = &twl4030_modules[3];
  445. pdev = platform_device_alloc("twl4030_rtc", -1);
  446. if (!pdev) {
  447. pr_debug("%s: can't alloc rtc dev\n", DRIVER_NAME);
  448. status = -ENOMEM;
  449. } else {
  450. pdev->dev.parent = &twl->client->dev;
  451. device_init_wakeup(&pdev->dev, 1);
  452. }
  453. /*
  454. * REVISIT platform_data here currently might use of
  455. * "msecure" line ... but for now we just expect board
  456. * setup to tell the chip "we are secure" at all times.
  457. * Eventually, Linux might become more aware of such
  458. * HW security concerns, and "least privilege".
  459. */
  460. /* RTC module IRQ */
  461. if (status == 0) {
  462. struct resource r = {
  463. .start = pdata->irq_base + 8 + 3,
  464. .flags = IORESOURCE_IRQ,
  465. };
  466. status = platform_device_add_resources(pdev, &r, 1);
  467. }
  468. if (status == 0)
  469. status = platform_device_add(pdev);
  470. if (status < 0) {
  471. platform_device_put(pdev);
  472. dev_dbg(&twl->client->dev,
  473. "can't create rtc dev, %d\n",
  474. status);
  475. goto err;
  476. }
  477. }
  478. if (twl_has_usb() && pdata->usb) {
  479. twl = &twl4030_modules[0];
  480. pdev = platform_device_alloc("twl4030_usb", -1);
  481. if (!pdev) {
  482. pr_debug("%s: can't alloc usb dev\n", DRIVER_NAME);
  483. status = -ENOMEM;
  484. goto err;
  485. }
  486. if (status == 0) {
  487. pdev->dev.parent = &twl->client->dev;
  488. device_init_wakeup(&pdev->dev, 1);
  489. status = platform_device_add_data(pdev, pdata->usb,
  490. sizeof(*pdata->usb));
  491. if (status < 0) {
  492. platform_device_put(pdev);
  493. dev_dbg(&twl->client->dev,
  494. "can't add usb data, %d\n",
  495. status);
  496. goto err;
  497. }
  498. }
  499. if (status == 0) {
  500. struct resource r = {
  501. .start = pdata->irq_base + 8 + 2,
  502. .flags = IORESOURCE_IRQ,
  503. };
  504. status = platform_device_add_resources(pdev, &r, 1);
  505. }
  506. if (status == 0)
  507. status = platform_device_add(pdev);
  508. if (status < 0) {
  509. platform_device_put(pdev);
  510. dev_dbg(&twl->client->dev,
  511. "can't create usb dev, %d\n",
  512. status);
  513. }
  514. }
  515. err:
  516. if (status)
  517. pr_err("failed to add twl4030's children (status %d)\n", status);
  518. return status;
  519. }
  520. /*----------------------------------------------------------------------*/
  521. /*
  522. * These three functions initialize the on-chip clock framework,
  523. * letting it generate the right frequencies for USB, MADC, and
  524. * other purposes.
  525. */
  526. static inline int __init protect_pm_master(void)
  527. {
  528. int e = 0;
  529. e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
  530. R_PROTECT_KEY);
  531. return e;
  532. }
  533. static inline int __init unprotect_pm_master(void)
  534. {
  535. int e = 0;
  536. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
  537. R_PROTECT_KEY);
  538. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
  539. R_PROTECT_KEY);
  540. return e;
  541. }
  542. static void __init clocks_init(void)
  543. {
  544. int e = 0;
  545. struct clk *osc;
  546. u32 rate;
  547. u8 ctrl = HFCLK_FREQ_26_MHZ;
  548. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  549. if (cpu_is_omap2430())
  550. osc = clk_get(NULL, "osc_ck");
  551. else
  552. osc = clk_get(NULL, "osc_sys_ck");
  553. #else
  554. /* REVISIT for non-OMAP systems, pass the clock rate from
  555. * board init code, using platform_data.
  556. */
  557. osc = ERR_PTR(-EIO);
  558. #endif
  559. if (IS_ERR(osc)) {
  560. printk(KERN_WARNING "Skipping twl4030 internal clock init and "
  561. "using bootloader value (unknown osc rate)\n");
  562. return;
  563. }
  564. rate = clk_get_rate(osc);
  565. clk_put(osc);
  566. switch (rate) {
  567. case 19200000:
  568. ctrl = HFCLK_FREQ_19p2_MHZ;
  569. break;
  570. case 26000000:
  571. ctrl = HFCLK_FREQ_26_MHZ;
  572. break;
  573. case 38400000:
  574. ctrl = HFCLK_FREQ_38p4_MHZ;
  575. break;
  576. }
  577. ctrl |= HIGH_PERF_SQ;
  578. e |= unprotect_pm_master();
  579. /* effect->MADC+USB ck en */
  580. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  581. e |= protect_pm_master();
  582. if (e < 0)
  583. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  584. }
  585. /*----------------------------------------------------------------------*/
  586. int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  587. int twl_exit_irq(void);
  588. static int twl4030_remove(struct i2c_client *client)
  589. {
  590. unsigned i;
  591. int status;
  592. status = twl_exit_irq();
  593. if (status < 0)
  594. return status;
  595. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  596. struct twl4030_client *twl = &twl4030_modules[i];
  597. if (twl->client && twl->client != client)
  598. i2c_unregister_device(twl->client);
  599. twl4030_modules[i].client = NULL;
  600. }
  601. inuse = false;
  602. return 0;
  603. }
  604. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  605. static int
  606. twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
  607. {
  608. int status;
  609. unsigned i;
  610. struct twl4030_platform_data *pdata = client->dev.platform_data;
  611. if (!pdata) {
  612. dev_dbg(&client->dev, "no platform data?\n");
  613. return -EINVAL;
  614. }
  615. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  616. dev_dbg(&client->dev, "can't talk I2C?\n");
  617. return -EIO;
  618. }
  619. if (inuse) {
  620. dev_dbg(&client->dev, "driver is already in use\n");
  621. return -EBUSY;
  622. }
  623. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  624. struct twl4030_client *twl = &twl4030_modules[i];
  625. twl->address = client->addr + i;
  626. if (i == 0)
  627. twl->client = client;
  628. else {
  629. twl->client = i2c_new_dummy(client->adapter,
  630. twl->address);
  631. if (!twl->client) {
  632. dev_err(&twl->client->dev,
  633. "can't attach client %d\n", i);
  634. status = -ENOMEM;
  635. goto fail;
  636. }
  637. strlcpy(twl->client->name, id->name,
  638. sizeof(twl->client->name));
  639. }
  640. mutex_init(&twl->xfer_lock);
  641. }
  642. inuse = true;
  643. /* setup clock framework */
  644. clocks_init();
  645. /* Maybe init the T2 Interrupt subsystem */
  646. if (client->irq
  647. && pdata->irq_base
  648. && pdata->irq_end > pdata->irq_base) {
  649. status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
  650. if (status < 0)
  651. goto fail;
  652. }
  653. status = add_children(pdata);
  654. fail:
  655. if (status < 0)
  656. twl4030_remove(client);
  657. return status;
  658. }
  659. static const struct i2c_device_id twl4030_ids[] = {
  660. { "twl4030", 0 }, /* "Triton 2" */
  661. { "tps65950", 0 }, /* catalog version of twl4030 */
  662. { "tps65930", 0 }, /* fewer LDOs and DACs; no charger */
  663. { "tps65920", 0 }, /* fewer LDOs; no codec or charger */
  664. { "twl5030", 0 }, /* T2 updated */
  665. { /* end of list */ },
  666. };
  667. MODULE_DEVICE_TABLE(i2c, twl4030_ids);
  668. /* One Client Driver , 4 Clients */
  669. static struct i2c_driver twl4030_driver = {
  670. .driver.name = DRIVER_NAME,
  671. .id_table = twl4030_ids,
  672. .probe = twl4030_probe,
  673. .remove = twl4030_remove,
  674. };
  675. static int __init twl4030_init(void)
  676. {
  677. return i2c_add_driver(&twl4030_driver);
  678. }
  679. subsys_initcall(twl4030_init);
  680. static void __exit twl4030_exit(void)
  681. {
  682. i2c_del_driver(&twl4030_driver);
  683. }
  684. module_exit(twl4030_exit);
  685. MODULE_AUTHOR("Texas Instruments, Inc.");
  686. MODULE_DESCRIPTION("I2C Core interface for TWL4030");
  687. MODULE_LICENSE("GPL");