twl-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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/regulator/machine.h>
  35. #include <linux/i2c.h>
  36. #include <linux/i2c/twl.h>
  37. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  38. #include <plat/cpu.h>
  39. #endif
  40. /*
  41. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  42. * Management and System Companion Device" chips originally designed for
  43. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  44. * often at around 3 Mbit/sec, including for interrupt handling.
  45. *
  46. * This driver core provides genirq support for the interrupts emitted,
  47. * by the various modules, and exports register access primitives.
  48. *
  49. * FIXME this driver currently requires use of the first interrupt line
  50. * (and associated registers).
  51. */
  52. #define DRIVER_NAME "twl4030"
  53. #if defined(CONFIG_TWL4030_BCI_BATTERY) || \
  54. defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
  55. #define twl_has_bci() true
  56. #else
  57. #define twl_has_bci() false
  58. #endif
  59. #if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
  60. #define twl_has_keypad() true
  61. #else
  62. #define twl_has_keypad() false
  63. #endif
  64. #if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
  65. #define twl_has_gpio() true
  66. #else
  67. #define twl_has_gpio() false
  68. #endif
  69. #if defined(CONFIG_REGULATOR_TWL4030) \
  70. || defined(CONFIG_REGULATOR_TWL4030_MODULE)
  71. #define twl_has_regulator() true
  72. #else
  73. #define twl_has_regulator() false
  74. #endif
  75. #if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
  76. #define twl_has_madc() true
  77. #else
  78. #define twl_has_madc() false
  79. #endif
  80. #ifdef CONFIG_TWL4030_POWER
  81. #define twl_has_power() true
  82. #else
  83. #define twl_has_power() false
  84. #endif
  85. #if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
  86. #define twl_has_rtc() true
  87. #else
  88. #define twl_has_rtc() false
  89. #endif
  90. #if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
  91. #define twl_has_usb() true
  92. #else
  93. #define twl_has_usb() false
  94. #endif
  95. #if defined(CONFIG_TWL4030_WATCHDOG) || \
  96. defined(CONFIG_TWL4030_WATCHDOG_MODULE)
  97. #define twl_has_watchdog() true
  98. #else
  99. #define twl_has_watchdog() false
  100. #endif
  101. #if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE)
  102. #define twl_has_codec() true
  103. #else
  104. #define twl_has_codec() false
  105. #endif
  106. /* Triton Core internal information (BEGIN) */
  107. /* Last - for index max*/
  108. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  109. #define TWL4030_NUM_SLAVES 4
  110. #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
  111. || defined(CONFIG_INPUT_TWL4030_PWBUTTON_MODULE)
  112. #define twl_has_pwrbutton() true
  113. #else
  114. #define twl_has_pwrbutton() false
  115. #endif
  116. /* Base Address defns for twl4030_map[] */
  117. /* subchip/slave 0 - USB ID */
  118. #define TWL4030_BASEADD_USB 0x0000
  119. /* subchip/slave 1 - AUD ID */
  120. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  121. #define TWL4030_BASEADD_GPIO 0x0098
  122. #define TWL4030_BASEADD_INTBR 0x0085
  123. #define TWL4030_BASEADD_PIH 0x0080
  124. #define TWL4030_BASEADD_TEST 0x004C
  125. /* subchip/slave 2 - AUX ID */
  126. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  127. #define TWL4030_BASEADD_LED 0x00EE
  128. #define TWL4030_BASEADD_MADC 0x0000
  129. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  130. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  131. #define TWL4030_BASEADD_PWM0 0x00F8
  132. #define TWL4030_BASEADD_PWM1 0x00FB
  133. #define TWL4030_BASEADD_PWMA 0x00EF
  134. #define TWL4030_BASEADD_PWMB 0x00F1
  135. #define TWL4030_BASEADD_KEYPAD 0x00D2
  136. #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
  137. #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
  138. one */
  139. /* subchip/slave 3 - POWER ID */
  140. #define TWL4030_BASEADD_BACKUP 0x0014
  141. #define TWL4030_BASEADD_INT 0x002E
  142. #define TWL4030_BASEADD_PM_MASTER 0x0036
  143. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  144. #define TWL4030_BASEADD_RTC 0x001C
  145. #define TWL4030_BASEADD_SECURED_REG 0x0000
  146. /* Triton Core internal information (END) */
  147. /* Few power values */
  148. #define R_CFG_BOOT 0x05
  149. #define R_PROTECT_KEY 0x0E
  150. /* access control values for R_PROTECT_KEY */
  151. #define KEY_UNLOCK1 0xce
  152. #define KEY_UNLOCK2 0xec
  153. #define KEY_LOCK 0x00
  154. /* some fields in R_CFG_BOOT */
  155. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  156. #define HFCLK_FREQ_26_MHZ (2 << 0)
  157. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  158. #define HIGH_PERF_SQ (1 << 3)
  159. #define CK32K_LOWPWR_EN (1 << 7)
  160. /* chip-specific feature flags, for i2c_device_id.driver_data */
  161. #define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
  162. #define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
  163. #define TWL5031 BIT(2) /* twl5031 has different registers */
  164. /*----------------------------------------------------------------------*/
  165. /* is driver active, bound to a chip? */
  166. static bool inuse;
  167. /* Structure for each TWL4030 Slave */
  168. struct twl4030_client {
  169. struct i2c_client *client;
  170. u8 address;
  171. /* max numb of i2c_msg required is for read =2 */
  172. struct i2c_msg xfer_msg[2];
  173. /* To lock access to xfer_msg */
  174. struct mutex xfer_lock;
  175. };
  176. static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
  177. /* mapping the module id to slave id and base address */
  178. struct twl4030mapping {
  179. unsigned char sid; /* Slave ID */
  180. unsigned char base; /* base address */
  181. };
  182. static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  183. /*
  184. * NOTE: don't change this table without updating the
  185. * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
  186. * so they continue to match the order in this table.
  187. */
  188. { 0, TWL4030_BASEADD_USB },
  189. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  190. { 1, TWL4030_BASEADD_GPIO },
  191. { 1, TWL4030_BASEADD_INTBR },
  192. { 1, TWL4030_BASEADD_PIH },
  193. { 1, TWL4030_BASEADD_TEST },
  194. { 2, TWL4030_BASEADD_KEYPAD },
  195. { 2, TWL4030_BASEADD_MADC },
  196. { 2, TWL4030_BASEADD_INTERRUPTS },
  197. { 2, TWL4030_BASEADD_LED },
  198. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  199. { 2, TWL4030_BASEADD_PRECHARGE },
  200. { 2, TWL4030_BASEADD_PWM0 },
  201. { 2, TWL4030_BASEADD_PWM1 },
  202. { 2, TWL4030_BASEADD_PWMA },
  203. { 2, TWL4030_BASEADD_PWMB },
  204. { 2, TWL5031_BASEADD_ACCESSORY },
  205. { 2, TWL5031_BASEADD_INTERRUPTS },
  206. { 3, TWL4030_BASEADD_BACKUP },
  207. { 3, TWL4030_BASEADD_INT },
  208. { 3, TWL4030_BASEADD_PM_MASTER },
  209. { 3, TWL4030_BASEADD_PM_RECEIVER },
  210. { 3, TWL4030_BASEADD_RTC },
  211. { 3, TWL4030_BASEADD_SECURED_REG },
  212. };
  213. /*----------------------------------------------------------------------*/
  214. /* Exported Functions */
  215. /**
  216. * twl4030_i2c_write - Writes a n bit register in TWL4030
  217. * @mod_no: module number
  218. * @value: an array of num_bytes+1 containing data to write
  219. * @reg: register address (just offset will do)
  220. * @num_bytes: number of bytes to transfer
  221. *
  222. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  223. * valid data starts at Offset 1.
  224. *
  225. * Returns the result of operation - 0 is success
  226. */
  227. int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  228. {
  229. int ret;
  230. int sid;
  231. struct twl4030_client *twl;
  232. struct i2c_msg *msg;
  233. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  234. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  235. return -EPERM;
  236. }
  237. sid = twl4030_map[mod_no].sid;
  238. twl = &twl4030_modules[sid];
  239. if (unlikely(!inuse)) {
  240. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  241. return -EPERM;
  242. }
  243. mutex_lock(&twl->xfer_lock);
  244. /*
  245. * [MSG1]: fill the register address data
  246. * fill the data Tx buffer
  247. */
  248. msg = &twl->xfer_msg[0];
  249. msg->addr = twl->address;
  250. msg->len = num_bytes + 1;
  251. msg->flags = 0;
  252. msg->buf = value;
  253. /* over write the first byte of buffer with the register address */
  254. *value = twl4030_map[mod_no].base + reg;
  255. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  256. mutex_unlock(&twl->xfer_lock);
  257. /* i2c_transfer returns number of messages transferred */
  258. if (ret != 1) {
  259. pr_err("%s: i2c_write failed to transfer all messages\n",
  260. DRIVER_NAME);
  261. if (ret < 0)
  262. return ret;
  263. else
  264. return -EIO;
  265. } else {
  266. return 0;
  267. }
  268. }
  269. EXPORT_SYMBOL(twl4030_i2c_write);
  270. /**
  271. * twl4030_i2c_read - Reads a n bit register in TWL4030
  272. * @mod_no: module number
  273. * @value: an array of num_bytes containing data to be read
  274. * @reg: register address (just offset will do)
  275. * @num_bytes: number of bytes to transfer
  276. *
  277. * Returns result of operation - num_bytes is success else failure.
  278. */
  279. int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  280. {
  281. int ret;
  282. u8 val;
  283. int sid;
  284. struct twl4030_client *twl;
  285. struct i2c_msg *msg;
  286. if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
  287. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  288. return -EPERM;
  289. }
  290. sid = twl4030_map[mod_no].sid;
  291. twl = &twl4030_modules[sid];
  292. if (unlikely(!inuse)) {
  293. pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
  294. return -EPERM;
  295. }
  296. mutex_lock(&twl->xfer_lock);
  297. /* [MSG1] fill the register address data */
  298. msg = &twl->xfer_msg[0];
  299. msg->addr = twl->address;
  300. msg->len = 1;
  301. msg->flags = 0; /* Read the register value */
  302. val = twl4030_map[mod_no].base + reg;
  303. msg->buf = &val;
  304. /* [MSG2] fill the data rx buffer */
  305. msg = &twl->xfer_msg[1];
  306. msg->addr = twl->address;
  307. msg->flags = I2C_M_RD; /* Read the register value */
  308. msg->len = num_bytes; /* only n bytes */
  309. msg->buf = value;
  310. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  311. mutex_unlock(&twl->xfer_lock);
  312. /* i2c_transfer returns number of messages transferred */
  313. if (ret != 2) {
  314. pr_err("%s: i2c_read failed to transfer all messages\n",
  315. DRIVER_NAME);
  316. if (ret < 0)
  317. return ret;
  318. else
  319. return -EIO;
  320. } else {
  321. return 0;
  322. }
  323. }
  324. EXPORT_SYMBOL(twl4030_i2c_read);
  325. /**
  326. * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
  327. * @mod_no: module number
  328. * @value: the value to be written 8 bit
  329. * @reg: register address (just offset will do)
  330. *
  331. * Returns result of operation - 0 is success
  332. */
  333. int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  334. {
  335. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  336. u8 temp_buffer[2] = { 0 };
  337. /* offset 1 contains the data */
  338. temp_buffer[1] = value;
  339. return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
  340. }
  341. EXPORT_SYMBOL(twl4030_i2c_write_u8);
  342. /**
  343. * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
  344. * @mod_no: module number
  345. * @value: the value read 8 bit
  346. * @reg: register address (just offset will do)
  347. *
  348. * Returns result of operation - 0 is success
  349. */
  350. int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  351. {
  352. return twl4030_i2c_read(mod_no, value, reg, 1);
  353. }
  354. EXPORT_SYMBOL(twl4030_i2c_read_u8);
  355. /*----------------------------------------------------------------------*/
  356. static struct device *
  357. add_numbered_child(unsigned chip, const char *name, int num,
  358. void *pdata, unsigned pdata_len,
  359. bool can_wakeup, int irq0, int irq1)
  360. {
  361. struct platform_device *pdev;
  362. struct twl4030_client *twl = &twl4030_modules[chip];
  363. int status;
  364. pdev = platform_device_alloc(name, num);
  365. if (!pdev) {
  366. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  367. status = -ENOMEM;
  368. goto err;
  369. }
  370. device_init_wakeup(&pdev->dev, can_wakeup);
  371. pdev->dev.parent = &twl->client->dev;
  372. if (pdata) {
  373. status = platform_device_add_data(pdev, pdata, pdata_len);
  374. if (status < 0) {
  375. dev_dbg(&pdev->dev, "can't add platform_data\n");
  376. goto err;
  377. }
  378. }
  379. if (irq0) {
  380. struct resource r[2] = {
  381. { .start = irq0, .flags = IORESOURCE_IRQ, },
  382. { .start = irq1, .flags = IORESOURCE_IRQ, },
  383. };
  384. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  385. if (status < 0) {
  386. dev_dbg(&pdev->dev, "can't add irqs\n");
  387. goto err;
  388. }
  389. }
  390. status = platform_device_add(pdev);
  391. err:
  392. if (status < 0) {
  393. platform_device_put(pdev);
  394. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  395. return ERR_PTR(status);
  396. }
  397. return &pdev->dev;
  398. }
  399. static inline struct device *add_child(unsigned chip, const char *name,
  400. void *pdata, unsigned pdata_len,
  401. bool can_wakeup, int irq0, int irq1)
  402. {
  403. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  404. can_wakeup, irq0, irq1);
  405. }
  406. static struct device *
  407. add_regulator_linked(int num, struct regulator_init_data *pdata,
  408. struct regulator_consumer_supply *consumers,
  409. unsigned num_consumers)
  410. {
  411. /* regulator framework demands init_data ... */
  412. if (!pdata)
  413. return NULL;
  414. if (consumers) {
  415. pdata->consumer_supplies = consumers;
  416. pdata->num_consumer_supplies = num_consumers;
  417. }
  418. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  419. return add_numbered_child(3, "twl4030_reg", num,
  420. pdata, sizeof(*pdata), false, 0, 0);
  421. }
  422. static struct device *
  423. add_regulator(int num, struct regulator_init_data *pdata)
  424. {
  425. return add_regulator_linked(num, pdata, NULL, 0);
  426. }
  427. /*
  428. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  429. * for the PIH, and the next are for the PWR_INT SIH, since
  430. * that's how twl_init_irq() sets things up.
  431. */
  432. static int
  433. add_children(struct twl4030_platform_data *pdata, unsigned long features)
  434. {
  435. struct device *child;
  436. if (twl_has_bci() && pdata->bci &&
  437. !(features & (TPS_SUBSET | TWL5031))) {
  438. child = add_child(3, "twl4030_bci",
  439. pdata->bci, sizeof(*pdata->bci),
  440. false,
  441. /* irq0 = CHG_PRES, irq1 = BCI */
  442. pdata->irq_base + 8 + 1, pdata->irq_base + 2);
  443. if (IS_ERR(child))
  444. return PTR_ERR(child);
  445. }
  446. if (twl_has_gpio() && pdata->gpio) {
  447. child = add_child(1, "twl4030_gpio",
  448. pdata->gpio, sizeof(*pdata->gpio),
  449. false, pdata->irq_base + 0, 0);
  450. if (IS_ERR(child))
  451. return PTR_ERR(child);
  452. }
  453. if (twl_has_keypad() && pdata->keypad) {
  454. child = add_child(2, "twl4030_keypad",
  455. pdata->keypad, sizeof(*pdata->keypad),
  456. true, pdata->irq_base + 1, 0);
  457. if (IS_ERR(child))
  458. return PTR_ERR(child);
  459. }
  460. if (twl_has_madc() && pdata->madc) {
  461. child = add_child(2, "twl4030_madc",
  462. pdata->madc, sizeof(*pdata->madc),
  463. true, pdata->irq_base + 3, 0);
  464. if (IS_ERR(child))
  465. return PTR_ERR(child);
  466. }
  467. if (twl_has_rtc()) {
  468. /*
  469. * REVISIT platform_data here currently might expose the
  470. * "msecure" line ... but for now we just expect board
  471. * setup to tell the chip "it's always ok to SET_TIME".
  472. * Eventually, Linux might become more aware of such
  473. * HW security concerns, and "least privilege".
  474. */
  475. child = add_child(3, "twl4030_rtc",
  476. NULL, 0,
  477. true, pdata->irq_base + 8 + 3, 0);
  478. if (IS_ERR(child))
  479. return PTR_ERR(child);
  480. }
  481. if (twl_has_usb() && pdata->usb) {
  482. static struct regulator_consumer_supply usb1v5 = {
  483. .supply = "usb1v5",
  484. };
  485. static struct regulator_consumer_supply usb1v8 = {
  486. .supply = "usb1v8",
  487. };
  488. static struct regulator_consumer_supply usb3v1 = {
  489. .supply = "usb3v1",
  490. };
  491. /* First add the regulators so that they can be used by transceiver */
  492. if (twl_has_regulator()) {
  493. /* this is a template that gets copied */
  494. struct regulator_init_data usb_fixed = {
  495. .constraints.valid_modes_mask =
  496. REGULATOR_MODE_NORMAL
  497. | REGULATOR_MODE_STANDBY,
  498. .constraints.valid_ops_mask =
  499. REGULATOR_CHANGE_MODE
  500. | REGULATOR_CHANGE_STATUS,
  501. };
  502. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  503. &usb_fixed, &usb1v5, 1);
  504. if (IS_ERR(child))
  505. return PTR_ERR(child);
  506. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  507. &usb_fixed, &usb1v8, 1);
  508. if (IS_ERR(child))
  509. return PTR_ERR(child);
  510. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  511. &usb_fixed, &usb3v1, 1);
  512. if (IS_ERR(child))
  513. return PTR_ERR(child);
  514. }
  515. child = add_child(0, "twl4030_usb",
  516. pdata->usb, sizeof(*pdata->usb),
  517. true,
  518. /* irq0 = USB_PRES, irq1 = USB */
  519. pdata->irq_base + 8 + 2, pdata->irq_base + 4);
  520. if (IS_ERR(child))
  521. return PTR_ERR(child);
  522. /* we need to connect regulators to this transceiver */
  523. if (twl_has_regulator() && child) {
  524. usb1v5.dev = child;
  525. usb1v8.dev = child;
  526. usb3v1.dev = child;
  527. }
  528. }
  529. if (twl_has_watchdog()) {
  530. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  531. if (IS_ERR(child))
  532. return PTR_ERR(child);
  533. }
  534. if (twl_has_pwrbutton()) {
  535. child = add_child(1, "twl4030_pwrbutton",
  536. NULL, 0, true, pdata->irq_base + 8 + 0, 0);
  537. if (IS_ERR(child))
  538. return PTR_ERR(child);
  539. }
  540. if (twl_has_codec() && pdata->codec) {
  541. child = add_child(1, "twl4030_codec",
  542. pdata->codec, sizeof(*pdata->codec),
  543. false, 0, 0);
  544. if (IS_ERR(child))
  545. return PTR_ERR(child);
  546. }
  547. if (twl_has_regulator()) {
  548. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
  549. if (IS_ERR(child))
  550. return PTR_ERR(child);
  551. child = add_regulator(TWL4030_REG_VIO, pdata->vio);
  552. if (IS_ERR(child))
  553. return PTR_ERR(child);
  554. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1);
  555. if (IS_ERR(child))
  556. return PTR_ERR(child);
  557. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2);
  558. if (IS_ERR(child))
  559. return PTR_ERR(child);
  560. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
  561. if (IS_ERR(child))
  562. return PTR_ERR(child);
  563. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
  564. if (IS_ERR(child))
  565. return PTR_ERR(child);
  566. child = add_regulator((features & TWL4030_VAUX2)
  567. ? TWL4030_REG_VAUX2_4030
  568. : TWL4030_REG_VAUX2,
  569. pdata->vaux2);
  570. if (IS_ERR(child))
  571. return PTR_ERR(child);
  572. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1);
  573. if (IS_ERR(child))
  574. return PTR_ERR(child);
  575. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2);
  576. if (IS_ERR(child))
  577. return PTR_ERR(child);
  578. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig);
  579. if (IS_ERR(child))
  580. return PTR_ERR(child);
  581. }
  582. /* maybe add LDOs that are omitted on cost-reduced parts */
  583. if (twl_has_regulator() && !(features & TPS_SUBSET)) {
  584. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
  585. if (IS_ERR(child))
  586. return PTR_ERR(child);
  587. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
  588. if (IS_ERR(child))
  589. return PTR_ERR(child);
  590. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
  591. if (IS_ERR(child))
  592. return PTR_ERR(child);
  593. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
  594. if (IS_ERR(child))
  595. return PTR_ERR(child);
  596. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
  597. if (IS_ERR(child))
  598. return PTR_ERR(child);
  599. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
  600. if (IS_ERR(child))
  601. return PTR_ERR(child);
  602. }
  603. return 0;
  604. }
  605. /*----------------------------------------------------------------------*/
  606. /*
  607. * These three functions initialize the on-chip clock framework,
  608. * letting it generate the right frequencies for USB, MADC, and
  609. * other purposes.
  610. */
  611. static inline int __init protect_pm_master(void)
  612. {
  613. int e = 0;
  614. e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
  615. R_PROTECT_KEY);
  616. return e;
  617. }
  618. static inline int __init unprotect_pm_master(void)
  619. {
  620. int e = 0;
  621. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
  622. R_PROTECT_KEY);
  623. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
  624. R_PROTECT_KEY);
  625. return e;
  626. }
  627. static void clocks_init(struct device *dev,
  628. struct twl4030_clock_init_data *clock)
  629. {
  630. int e = 0;
  631. struct clk *osc;
  632. u32 rate;
  633. u8 ctrl = HFCLK_FREQ_26_MHZ;
  634. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  635. if (cpu_is_omap2430())
  636. osc = clk_get(dev, "osc_ck");
  637. else
  638. osc = clk_get(dev, "osc_sys_ck");
  639. if (IS_ERR(osc)) {
  640. printk(KERN_WARNING "Skipping twl4030 internal clock init and "
  641. "using bootloader value (unknown osc rate)\n");
  642. return;
  643. }
  644. rate = clk_get_rate(osc);
  645. clk_put(osc);
  646. #else
  647. /* REVISIT for non-OMAP systems, pass the clock rate from
  648. * board init code, using platform_data.
  649. */
  650. osc = ERR_PTR(-EIO);
  651. printk(KERN_WARNING "Skipping twl4030 internal clock init and "
  652. "using bootloader value (unknown osc rate)\n");
  653. return;
  654. #endif
  655. switch (rate) {
  656. case 19200000:
  657. ctrl = HFCLK_FREQ_19p2_MHZ;
  658. break;
  659. case 26000000:
  660. ctrl = HFCLK_FREQ_26_MHZ;
  661. break;
  662. case 38400000:
  663. ctrl = HFCLK_FREQ_38p4_MHZ;
  664. break;
  665. }
  666. ctrl |= HIGH_PERF_SQ;
  667. if (clock && clock->ck32k_lowpwr_enable)
  668. ctrl |= CK32K_LOWPWR_EN;
  669. e |= unprotect_pm_master();
  670. /* effect->MADC+USB ck en */
  671. e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  672. e |= protect_pm_master();
  673. if (e < 0)
  674. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  675. }
  676. /*----------------------------------------------------------------------*/
  677. int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
  678. int twl_exit_irq(void);
  679. int twl_init_chip_irq(const char *chip);
  680. static int twl4030_remove(struct i2c_client *client)
  681. {
  682. unsigned i;
  683. int status;
  684. status = twl_exit_irq();
  685. if (status < 0)
  686. return status;
  687. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  688. struct twl4030_client *twl = &twl4030_modules[i];
  689. if (twl->client && twl->client != client)
  690. i2c_unregister_device(twl->client);
  691. twl4030_modules[i].client = NULL;
  692. }
  693. inuse = false;
  694. return 0;
  695. }
  696. /* NOTE: this driver only handles a single twl4030/tps659x0 chip */
  697. static int __init
  698. twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
  699. {
  700. int status;
  701. unsigned i;
  702. struct twl4030_platform_data *pdata = client->dev.platform_data;
  703. if (!pdata) {
  704. dev_dbg(&client->dev, "no platform data?\n");
  705. return -EINVAL;
  706. }
  707. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  708. dev_dbg(&client->dev, "can't talk I2C?\n");
  709. return -EIO;
  710. }
  711. if (inuse) {
  712. dev_dbg(&client->dev, "driver is already in use\n");
  713. return -EBUSY;
  714. }
  715. for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
  716. struct twl4030_client *twl = &twl4030_modules[i];
  717. twl->address = client->addr + i;
  718. if (i == 0)
  719. twl->client = client;
  720. else {
  721. twl->client = i2c_new_dummy(client->adapter,
  722. twl->address);
  723. if (!twl->client) {
  724. dev_err(&client->dev,
  725. "can't attach client %d\n", i);
  726. status = -ENOMEM;
  727. goto fail;
  728. }
  729. }
  730. mutex_init(&twl->xfer_lock);
  731. }
  732. inuse = true;
  733. /* setup clock framework */
  734. clocks_init(&client->dev, pdata->clock);
  735. /* load power event scripts */
  736. if (twl_has_power() && pdata->power)
  737. twl4030_power_init(pdata->power);
  738. /* Maybe init the T2 Interrupt subsystem */
  739. if (client->irq
  740. && pdata->irq_base
  741. && pdata->irq_end > pdata->irq_base) {
  742. twl_init_chip_irq(id->name);
  743. status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
  744. if (status < 0)
  745. goto fail;
  746. }
  747. status = add_children(pdata, id->driver_data);
  748. fail:
  749. if (status < 0)
  750. twl4030_remove(client);
  751. return status;
  752. }
  753. static const struct i2c_device_id twl4030_ids[] = {
  754. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  755. { "twl5030", 0 }, /* T2 updated */
  756. { "twl5031", TWL5031 }, /* TWL5030 updated */
  757. { "tps65950", 0 }, /* catalog version of twl5030 */
  758. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  759. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  760. { /* end of list */ },
  761. };
  762. MODULE_DEVICE_TABLE(i2c, twl4030_ids);
  763. /* One Client Driver , 4 Clients */
  764. static struct i2c_driver twl4030_driver = {
  765. .driver.name = DRIVER_NAME,
  766. .id_table = twl4030_ids,
  767. .probe = twl4030_probe,
  768. .remove = twl4030_remove,
  769. };
  770. static int __init twl4030_init(void)
  771. {
  772. return i2c_add_driver(&twl4030_driver);
  773. }
  774. subsys_initcall(twl4030_init);
  775. static void __exit twl4030_exit(void)
  776. {
  777. i2c_del_driver(&twl4030_driver);
  778. }
  779. module_exit(twl4030_exit);
  780. MODULE_AUTHOR("Texas Instruments, Inc.");
  781. MODULE_DESCRIPTION("I2C Core interface for TWL4030");
  782. MODULE_LICENSE("GPL");