twl-core.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
  3. * and audio CODEC devices
  4. *
  5. * Copyright (C) 2005-2006 Texas Instruments, Inc.
  6. *
  7. * Modifications to defer interrupt handling to a kernel thread:
  8. * Copyright (C) 2006 MontaVista Software, Inc.
  9. *
  10. * Based on tlv320aic23.c:
  11. * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
  12. *
  13. * Code cleanup and modifications to IRQ handler.
  14. * by syed khasim <x0khasim@ti.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <linux/init.h>
  31. #include <linux/mutex.h>
  32. #include <linux/module.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/clk.h>
  35. #include <linux/err.h>
  36. #include <linux/device.h>
  37. #include <linux/of.h>
  38. #include <linux/of_irq.h>
  39. #include <linux/of_platform.h>
  40. #include <linux/irq.h>
  41. #include <linux/irqdomain.h>
  42. #include <linux/regulator/machine.h>
  43. #include <linux/i2c.h>
  44. #include <linux/i2c/twl.h>
  45. #include "twl-core.h"
  46. /*
  47. * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
  48. * Management and System Companion Device" chips originally designed for
  49. * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
  50. * often at around 3 Mbit/sec, including for interrupt handling.
  51. *
  52. * This driver core provides genirq support for the interrupts emitted,
  53. * by the various modules, and exports register access primitives.
  54. *
  55. * FIXME this driver currently requires use of the first interrupt line
  56. * (and associated registers).
  57. */
  58. #define DRIVER_NAME "twl"
  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. defined(CONFIG_TWL6030_USB) || defined(CONFIG_TWL6030_USB_MODULE)
  92. #define twl_has_usb() true
  93. #else
  94. #define twl_has_usb() false
  95. #endif
  96. #if defined(CONFIG_TWL4030_WATCHDOG) || \
  97. defined(CONFIG_TWL4030_WATCHDOG_MODULE)
  98. #define twl_has_watchdog() true
  99. #else
  100. #define twl_has_watchdog() false
  101. #endif
  102. #if defined(CONFIG_MFD_TWL4030_AUDIO) || \
  103. defined(CONFIG_MFD_TWL4030_AUDIO_MODULE)
  104. #define twl_has_codec() true
  105. #else
  106. #define twl_has_codec() false
  107. #endif
  108. #if defined(CONFIG_CHARGER_TWL4030) || defined(CONFIG_CHARGER_TWL4030_MODULE)
  109. #define twl_has_bci() true
  110. #else
  111. #define twl_has_bci() false
  112. #endif
  113. /* Triton Core internal information (BEGIN) */
  114. /* Last - for index max*/
  115. #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
  116. #define TWL_NUM_SLAVES 4
  117. #if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
  118. || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
  119. #define twl_has_pwrbutton() true
  120. #else
  121. #define twl_has_pwrbutton() false
  122. #endif
  123. #define SUB_CHIP_ID0 0
  124. #define SUB_CHIP_ID1 1
  125. #define SUB_CHIP_ID2 2
  126. #define SUB_CHIP_ID3 3
  127. #define SUB_CHIP_ID_INVAL 0xff
  128. #define TWL_MODULE_LAST TWL4030_MODULE_LAST
  129. /* Base Address defns for twl4030_map[] */
  130. /* subchip/slave 0 - USB ID */
  131. #define TWL4030_BASEADD_USB 0x0000
  132. /* subchip/slave 1 - AUD ID */
  133. #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
  134. #define TWL4030_BASEADD_GPIO 0x0098
  135. #define TWL4030_BASEADD_INTBR 0x0085
  136. #define TWL4030_BASEADD_PIH 0x0080
  137. #define TWL4030_BASEADD_TEST 0x004C
  138. /* subchip/slave 2 - AUX ID */
  139. #define TWL4030_BASEADD_INTERRUPTS 0x00B9
  140. #define TWL4030_BASEADD_LED 0x00EE
  141. #define TWL4030_BASEADD_MADC 0x0000
  142. #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
  143. #define TWL4030_BASEADD_PRECHARGE 0x00AA
  144. #define TWL4030_BASEADD_PWM0 0x00F8
  145. #define TWL4030_BASEADD_PWM1 0x00FB
  146. #define TWL4030_BASEADD_PWMA 0x00EF
  147. #define TWL4030_BASEADD_PWMB 0x00F1
  148. #define TWL4030_BASEADD_KEYPAD 0x00D2
  149. #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
  150. #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
  151. one */
  152. /* subchip/slave 3 - POWER ID */
  153. #define TWL4030_BASEADD_BACKUP 0x0014
  154. #define TWL4030_BASEADD_INT 0x002E
  155. #define TWL4030_BASEADD_PM_MASTER 0x0036
  156. #define TWL4030_BASEADD_PM_RECEIVER 0x005B
  157. #define TWL4030_BASEADD_RTC 0x001C
  158. #define TWL4030_BASEADD_SECURED_REG 0x0000
  159. /* Triton Core internal information (END) */
  160. /* subchip/slave 0 0x48 - POWER */
  161. #define TWL6030_BASEADD_RTC 0x0000
  162. #define TWL6030_BASEADD_MEM 0x0017
  163. #define TWL6030_BASEADD_PM_MASTER 0x001F
  164. #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
  165. #define TWL6030_BASEADD_PM_MISC 0x00E2
  166. #define TWL6030_BASEADD_PM_PUPD 0x00F0
  167. /* subchip/slave 1 0x49 - FEATURE */
  168. #define TWL6030_BASEADD_USB 0x0000
  169. #define TWL6030_BASEADD_GPADC_CTRL 0x002E
  170. #define TWL6030_BASEADD_AUX 0x0090
  171. #define TWL6030_BASEADD_PWM 0x00BA
  172. #define TWL6030_BASEADD_GASGAUGE 0x00C0
  173. #define TWL6030_BASEADD_PIH 0x00D0
  174. #define TWL6030_BASEADD_CHARGER 0x00E0
  175. #define TWL6025_BASEADD_CHARGER 0x00DA
  176. /* subchip/slave 2 0x4A - DFT */
  177. #define TWL6030_BASEADD_DIEID 0x00C0
  178. /* subchip/slave 3 0x4B - AUDIO */
  179. #define TWL6030_BASEADD_AUDIO 0x0000
  180. #define TWL6030_BASEADD_RSV 0x0000
  181. #define TWL6030_BASEADD_ZERO 0x0000
  182. /* Few power values */
  183. #define R_CFG_BOOT 0x05
  184. /* some fields in R_CFG_BOOT */
  185. #define HFCLK_FREQ_19p2_MHZ (1 << 0)
  186. #define HFCLK_FREQ_26_MHZ (2 << 0)
  187. #define HFCLK_FREQ_38p4_MHZ (3 << 0)
  188. #define HIGH_PERF_SQ (1 << 3)
  189. #define CK32K_LOWPWR_EN (1 << 7)
  190. /*----------------------------------------------------------------------*/
  191. /* is driver active, bound to a chip? */
  192. static bool inuse;
  193. /* TWL IDCODE Register value */
  194. static u32 twl_idcode;
  195. static unsigned int twl_id;
  196. unsigned int twl_rev(void)
  197. {
  198. return twl_id;
  199. }
  200. EXPORT_SYMBOL(twl_rev);
  201. /* Structure for each TWL4030/TWL6030 Slave */
  202. struct twl_client {
  203. struct i2c_client *client;
  204. u8 address;
  205. /* max numb of i2c_msg required is for read =2 */
  206. struct i2c_msg xfer_msg[2];
  207. /* To lock access to xfer_msg */
  208. struct mutex xfer_lock;
  209. };
  210. static struct twl_client twl_modules[TWL_NUM_SLAVES];
  211. /* mapping the module id to slave id and base address */
  212. struct twl_mapping {
  213. unsigned char sid; /* Slave ID */
  214. unsigned char base; /* base address */
  215. };
  216. static struct twl_mapping *twl_map;
  217. static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
  218. /*
  219. * NOTE: don't change this table without updating the
  220. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  221. * so they continue to match the order in this table.
  222. */
  223. { 0, TWL4030_BASEADD_USB },
  224. { 1, TWL4030_BASEADD_AUDIO_VOICE },
  225. { 1, TWL4030_BASEADD_GPIO },
  226. { 1, TWL4030_BASEADD_INTBR },
  227. { 1, TWL4030_BASEADD_PIH },
  228. { 1, TWL4030_BASEADD_TEST },
  229. { 2, TWL4030_BASEADD_KEYPAD },
  230. { 2, TWL4030_BASEADD_MADC },
  231. { 2, TWL4030_BASEADD_INTERRUPTS },
  232. { 2, TWL4030_BASEADD_LED },
  233. { 2, TWL4030_BASEADD_MAIN_CHARGE },
  234. { 2, TWL4030_BASEADD_PRECHARGE },
  235. { 2, TWL4030_BASEADD_PWM0 },
  236. { 2, TWL4030_BASEADD_PWM1 },
  237. { 2, TWL4030_BASEADD_PWMA },
  238. { 2, TWL4030_BASEADD_PWMB },
  239. { 2, TWL5031_BASEADD_ACCESSORY },
  240. { 2, TWL5031_BASEADD_INTERRUPTS },
  241. { 3, TWL4030_BASEADD_BACKUP },
  242. { 3, TWL4030_BASEADD_INT },
  243. { 3, TWL4030_BASEADD_PM_MASTER },
  244. { 3, TWL4030_BASEADD_PM_RECEIVER },
  245. { 3, TWL4030_BASEADD_RTC },
  246. { 3, TWL4030_BASEADD_SECURED_REG },
  247. };
  248. static struct twl_mapping twl6030_map[] = {
  249. /*
  250. * NOTE: don't change this table without updating the
  251. * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
  252. * so they continue to match the order in this table.
  253. */
  254. { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
  255. { SUB_CHIP_ID_INVAL, TWL6030_BASEADD_AUDIO },
  256. { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
  257. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  258. { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
  259. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  260. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  261. { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
  262. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  263. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  264. { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
  265. { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
  266. { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
  267. { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
  268. { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
  269. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  270. { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
  271. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  272. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  273. { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
  274. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
  275. { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
  276. { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
  277. { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
  278. { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
  279. };
  280. /*----------------------------------------------------------------------*/
  281. /* Exported Functions */
  282. /**
  283. * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
  284. * @mod_no: module number
  285. * @value: an array of num_bytes+1 containing data to write
  286. * @reg: register address (just offset will do)
  287. * @num_bytes: number of bytes to transfer
  288. *
  289. * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
  290. * valid data starts at Offset 1.
  291. *
  292. * Returns the result of operation - 0 is success
  293. */
  294. int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  295. {
  296. int ret;
  297. int sid;
  298. struct twl_client *twl;
  299. struct i2c_msg *msg;
  300. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  301. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  302. return -EPERM;
  303. }
  304. if (unlikely(!inuse)) {
  305. pr_err("%s: not initialized\n", DRIVER_NAME);
  306. return -EPERM;
  307. }
  308. sid = twl_map[mod_no].sid;
  309. if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
  310. pr_err("%s: module %d is not part of the pmic\n",
  311. DRIVER_NAME, mod_no);
  312. return -EINVAL;
  313. }
  314. twl = &twl_modules[sid];
  315. mutex_lock(&twl->xfer_lock);
  316. /*
  317. * [MSG1]: fill the register address data
  318. * fill the data Tx buffer
  319. */
  320. msg = &twl->xfer_msg[0];
  321. msg->addr = twl->address;
  322. msg->len = num_bytes + 1;
  323. msg->flags = 0;
  324. msg->buf = value;
  325. /* over write the first byte of buffer with the register address */
  326. *value = twl_map[mod_no].base + reg;
  327. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
  328. mutex_unlock(&twl->xfer_lock);
  329. /* i2c_transfer returns number of messages transferred */
  330. if (ret != 1) {
  331. pr_err("%s: i2c_write failed to transfer all messages\n",
  332. DRIVER_NAME);
  333. if (ret < 0)
  334. return ret;
  335. else
  336. return -EIO;
  337. } else {
  338. return 0;
  339. }
  340. }
  341. EXPORT_SYMBOL(twl_i2c_write);
  342. /**
  343. * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
  344. * @mod_no: module number
  345. * @value: an array of num_bytes containing data to be read
  346. * @reg: register address (just offset will do)
  347. * @num_bytes: number of bytes to transfer
  348. *
  349. * Returns result of operation - num_bytes is success else failure.
  350. */
  351. int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
  352. {
  353. int ret;
  354. u8 val;
  355. int sid;
  356. struct twl_client *twl;
  357. struct i2c_msg *msg;
  358. if (unlikely(mod_no > TWL_MODULE_LAST)) {
  359. pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
  360. return -EPERM;
  361. }
  362. if (unlikely(!inuse)) {
  363. pr_err("%s: not initialized\n", DRIVER_NAME);
  364. return -EPERM;
  365. }
  366. sid = twl_map[mod_no].sid;
  367. if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
  368. pr_err("%s: module %d is not part of the pmic\n",
  369. DRIVER_NAME, mod_no);
  370. return -EINVAL;
  371. }
  372. twl = &twl_modules[sid];
  373. mutex_lock(&twl->xfer_lock);
  374. /* [MSG1] fill the register address data */
  375. msg = &twl->xfer_msg[0];
  376. msg->addr = twl->address;
  377. msg->len = 1;
  378. msg->flags = 0; /* Read the register value */
  379. val = twl_map[mod_no].base + reg;
  380. msg->buf = &val;
  381. /* [MSG2] fill the data rx buffer */
  382. msg = &twl->xfer_msg[1];
  383. msg->addr = twl->address;
  384. msg->flags = I2C_M_RD; /* Read the register value */
  385. msg->len = num_bytes; /* only n bytes */
  386. msg->buf = value;
  387. ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
  388. mutex_unlock(&twl->xfer_lock);
  389. /* i2c_transfer returns number of messages transferred */
  390. if (ret != 2) {
  391. pr_err("%s: i2c_read failed to transfer all messages\n",
  392. DRIVER_NAME);
  393. if (ret < 0)
  394. return ret;
  395. else
  396. return -EIO;
  397. } else {
  398. return 0;
  399. }
  400. }
  401. EXPORT_SYMBOL(twl_i2c_read);
  402. /**
  403. * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
  404. * @mod_no: module number
  405. * @value: the value to be written 8 bit
  406. * @reg: register address (just offset will do)
  407. *
  408. * Returns result of operation - 0 is success
  409. */
  410. int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
  411. {
  412. /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
  413. u8 temp_buffer[2] = { 0 };
  414. /* offset 1 contains the data */
  415. temp_buffer[1] = value;
  416. return twl_i2c_write(mod_no, temp_buffer, reg, 1);
  417. }
  418. EXPORT_SYMBOL(twl_i2c_write_u8);
  419. /**
  420. * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
  421. * @mod_no: module number
  422. * @value: the value read 8 bit
  423. * @reg: register address (just offset will do)
  424. *
  425. * Returns result of operation - 0 is success
  426. */
  427. int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
  428. {
  429. return twl_i2c_read(mod_no, value, reg, 1);
  430. }
  431. EXPORT_SYMBOL(twl_i2c_read_u8);
  432. /*----------------------------------------------------------------------*/
  433. /**
  434. * twl_read_idcode_register - API to read the IDCODE register.
  435. *
  436. * Unlocks the IDCODE register and read the 32 bit value.
  437. */
  438. static int twl_read_idcode_register(void)
  439. {
  440. int err;
  441. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
  442. REG_UNLOCK_TEST_REG);
  443. if (err) {
  444. pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
  445. goto fail;
  446. }
  447. err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
  448. REG_IDCODE_7_0, 4);
  449. if (err) {
  450. pr_err("TWL4030: unable to read IDCODE -%d\n", err);
  451. goto fail;
  452. }
  453. err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
  454. if (err)
  455. pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
  456. fail:
  457. return err;
  458. }
  459. /**
  460. * twl_get_type - API to get TWL Si type.
  461. *
  462. * Api to get the TWL Si type from IDCODE value.
  463. */
  464. int twl_get_type(void)
  465. {
  466. return TWL_SIL_TYPE(twl_idcode);
  467. }
  468. EXPORT_SYMBOL_GPL(twl_get_type);
  469. /**
  470. * twl_get_version - API to get TWL Si version.
  471. *
  472. * Api to get the TWL Si version from IDCODE value.
  473. */
  474. int twl_get_version(void)
  475. {
  476. return TWL_SIL_REV(twl_idcode);
  477. }
  478. EXPORT_SYMBOL_GPL(twl_get_version);
  479. static struct device *
  480. add_numbered_child(unsigned chip, const char *name, int num,
  481. void *pdata, unsigned pdata_len,
  482. bool can_wakeup, int irq0, int irq1)
  483. {
  484. struct platform_device *pdev;
  485. struct twl_client *twl = &twl_modules[chip];
  486. int status;
  487. pdev = platform_device_alloc(name, num);
  488. if (!pdev) {
  489. dev_dbg(&twl->client->dev, "can't alloc dev\n");
  490. status = -ENOMEM;
  491. goto err;
  492. }
  493. device_init_wakeup(&pdev->dev, can_wakeup);
  494. pdev->dev.parent = &twl->client->dev;
  495. if (pdata) {
  496. status = platform_device_add_data(pdev, pdata, pdata_len);
  497. if (status < 0) {
  498. dev_dbg(&pdev->dev, "can't add platform_data\n");
  499. goto err;
  500. }
  501. }
  502. if (irq0) {
  503. struct resource r[2] = {
  504. { .start = irq0, .flags = IORESOURCE_IRQ, },
  505. { .start = irq1, .flags = IORESOURCE_IRQ, },
  506. };
  507. status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
  508. if (status < 0) {
  509. dev_dbg(&pdev->dev, "can't add irqs\n");
  510. goto err;
  511. }
  512. }
  513. status = platform_device_add(pdev);
  514. err:
  515. if (status < 0) {
  516. platform_device_put(pdev);
  517. dev_err(&twl->client->dev, "can't add %s dev\n", name);
  518. return ERR_PTR(status);
  519. }
  520. return &pdev->dev;
  521. }
  522. static inline struct device *add_child(unsigned chip, const char *name,
  523. void *pdata, unsigned pdata_len,
  524. bool can_wakeup, int irq0, int irq1)
  525. {
  526. return add_numbered_child(chip, name, -1, pdata, pdata_len,
  527. can_wakeup, irq0, irq1);
  528. }
  529. static struct device *
  530. add_regulator_linked(int num, struct regulator_init_data *pdata,
  531. struct regulator_consumer_supply *consumers,
  532. unsigned num_consumers, unsigned long features)
  533. {
  534. unsigned sub_chip_id;
  535. struct twl_regulator_driver_data drv_data;
  536. /* regulator framework demands init_data ... */
  537. if (!pdata)
  538. return NULL;
  539. if (consumers) {
  540. pdata->consumer_supplies = consumers;
  541. pdata->num_consumer_supplies = num_consumers;
  542. }
  543. if (pdata->driver_data) {
  544. /* If we have existing drv_data, just add the flags */
  545. struct twl_regulator_driver_data *tmp;
  546. tmp = pdata->driver_data;
  547. tmp->features |= features;
  548. } else {
  549. /* add new driver data struct, used only during init */
  550. drv_data.features = features;
  551. drv_data.set_voltage = NULL;
  552. drv_data.get_voltage = NULL;
  553. drv_data.data = NULL;
  554. pdata->driver_data = &drv_data;
  555. }
  556. /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
  557. sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
  558. return add_numbered_child(sub_chip_id, "twl_reg", num,
  559. pdata, sizeof(*pdata), false, 0, 0);
  560. }
  561. static struct device *
  562. add_regulator(int num, struct regulator_init_data *pdata,
  563. unsigned long features)
  564. {
  565. return add_regulator_linked(num, pdata, NULL, 0, features);
  566. }
  567. /*
  568. * NOTE: We know the first 8 IRQs after pdata->base_irq are
  569. * for the PIH, and the next are for the PWR_INT SIH, since
  570. * that's how twl_init_irq() sets things up.
  571. */
  572. static int
  573. add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
  574. unsigned long features)
  575. {
  576. struct device *child;
  577. unsigned sub_chip_id;
  578. if (twl_has_gpio() && pdata->gpio) {
  579. child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
  580. pdata->gpio, sizeof(*pdata->gpio),
  581. false, irq_base + GPIO_INTR_OFFSET, 0);
  582. if (IS_ERR(child))
  583. return PTR_ERR(child);
  584. }
  585. if (twl_has_keypad() && pdata->keypad) {
  586. child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
  587. pdata->keypad, sizeof(*pdata->keypad),
  588. true, irq_base + KEYPAD_INTR_OFFSET, 0);
  589. if (IS_ERR(child))
  590. return PTR_ERR(child);
  591. }
  592. if (twl_has_madc() && pdata->madc) {
  593. child = add_child(2, "twl4030_madc",
  594. pdata->madc, sizeof(*pdata->madc),
  595. true, irq_base + MADC_INTR_OFFSET, 0);
  596. if (IS_ERR(child))
  597. return PTR_ERR(child);
  598. }
  599. if (twl_has_rtc()) {
  600. /*
  601. * REVISIT platform_data here currently might expose the
  602. * "msecure" line ... but for now we just expect board
  603. * setup to tell the chip "it's always ok to SET_TIME".
  604. * Eventually, Linux might become more aware of such
  605. * HW security concerns, and "least privilege".
  606. */
  607. sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
  608. child = add_child(sub_chip_id, "twl_rtc",
  609. NULL, 0,
  610. true, irq_base + RTC_INTR_OFFSET, 0);
  611. if (IS_ERR(child))
  612. return PTR_ERR(child);
  613. }
  614. if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
  615. static struct regulator_consumer_supply usb1v5 = {
  616. .supply = "usb1v5",
  617. };
  618. static struct regulator_consumer_supply usb1v8 = {
  619. .supply = "usb1v8",
  620. };
  621. static struct regulator_consumer_supply usb3v1 = {
  622. .supply = "usb3v1",
  623. };
  624. /* First add the regulators so that they can be used by transceiver */
  625. if (twl_has_regulator()) {
  626. /* this is a template that gets copied */
  627. struct regulator_init_data usb_fixed = {
  628. .constraints.valid_modes_mask =
  629. REGULATOR_MODE_NORMAL
  630. | REGULATOR_MODE_STANDBY,
  631. .constraints.valid_ops_mask =
  632. REGULATOR_CHANGE_MODE
  633. | REGULATOR_CHANGE_STATUS,
  634. };
  635. child = add_regulator_linked(TWL4030_REG_VUSB1V5,
  636. &usb_fixed, &usb1v5, 1,
  637. features);
  638. if (IS_ERR(child))
  639. return PTR_ERR(child);
  640. child = add_regulator_linked(TWL4030_REG_VUSB1V8,
  641. &usb_fixed, &usb1v8, 1,
  642. features);
  643. if (IS_ERR(child))
  644. return PTR_ERR(child);
  645. child = add_regulator_linked(TWL4030_REG_VUSB3V1,
  646. &usb_fixed, &usb3v1, 1,
  647. features);
  648. if (IS_ERR(child))
  649. return PTR_ERR(child);
  650. }
  651. child = add_child(0, "twl4030_usb",
  652. pdata->usb, sizeof(*pdata->usb),
  653. true,
  654. /* irq0 = USB_PRES, irq1 = USB */
  655. irq_base + USB_PRES_INTR_OFFSET,
  656. irq_base + USB_INTR_OFFSET);
  657. if (IS_ERR(child))
  658. return PTR_ERR(child);
  659. /* we need to connect regulators to this transceiver */
  660. if (twl_has_regulator() && child) {
  661. usb1v5.dev_name = dev_name(child);
  662. usb1v8.dev_name = dev_name(child);
  663. usb3v1.dev_name = dev_name(child);
  664. }
  665. }
  666. if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
  667. static struct regulator_consumer_supply usb3v3;
  668. int regulator;
  669. if (twl_has_regulator()) {
  670. /* this is a template that gets copied */
  671. struct regulator_init_data usb_fixed = {
  672. .constraints.valid_modes_mask =
  673. REGULATOR_MODE_NORMAL
  674. | REGULATOR_MODE_STANDBY,
  675. .constraints.valid_ops_mask =
  676. REGULATOR_CHANGE_MODE
  677. | REGULATOR_CHANGE_STATUS,
  678. };
  679. if (features & TWL6025_SUBCLASS) {
  680. usb3v3.supply = "ldousb";
  681. regulator = TWL6025_REG_LDOUSB;
  682. } else {
  683. usb3v3.supply = "vusb";
  684. regulator = TWL6030_REG_VUSB;
  685. }
  686. child = add_regulator_linked(regulator, &usb_fixed,
  687. &usb3v3, 1,
  688. features);
  689. if (IS_ERR(child))
  690. return PTR_ERR(child);
  691. }
  692. pdata->usb->features = features;
  693. child = add_child(0, "twl6030_usb",
  694. pdata->usb, sizeof(*pdata->usb),
  695. true,
  696. /* irq1 = VBUS_PRES, irq0 = USB ID */
  697. irq_base + USBOTG_INTR_OFFSET,
  698. irq_base + USB_PRES_INTR_OFFSET);
  699. if (IS_ERR(child))
  700. return PTR_ERR(child);
  701. /* we need to connect regulators to this transceiver */
  702. if (twl_has_regulator() && child)
  703. usb3v3.dev_name = dev_name(child);
  704. } else if (twl_has_regulator() && twl_class_is_6030()) {
  705. if (features & TWL6025_SUBCLASS)
  706. child = add_regulator(TWL6025_REG_LDOUSB,
  707. pdata->ldousb, features);
  708. else
  709. child = add_regulator(TWL6030_REG_VUSB,
  710. pdata->vusb, features);
  711. if (IS_ERR(child))
  712. return PTR_ERR(child);
  713. }
  714. if (twl_has_watchdog() && twl_class_is_4030()) {
  715. child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
  716. if (IS_ERR(child))
  717. return PTR_ERR(child);
  718. }
  719. if (twl_has_pwrbutton() && twl_class_is_4030()) {
  720. child = add_child(1, "twl4030_pwrbutton",
  721. NULL, 0, true, irq_base + 8 + 0, 0);
  722. if (IS_ERR(child))
  723. return PTR_ERR(child);
  724. }
  725. if (twl_has_codec() && pdata->audio && twl_class_is_4030()) {
  726. sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
  727. child = add_child(sub_chip_id, "twl4030-audio",
  728. pdata->audio, sizeof(*pdata->audio),
  729. false, 0, 0);
  730. if (IS_ERR(child))
  731. return PTR_ERR(child);
  732. }
  733. /* twl4030 regulators */
  734. if (twl_has_regulator() && twl_class_is_4030()) {
  735. child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
  736. features);
  737. if (IS_ERR(child))
  738. return PTR_ERR(child);
  739. child = add_regulator(TWL4030_REG_VIO, pdata->vio,
  740. features);
  741. if (IS_ERR(child))
  742. return PTR_ERR(child);
  743. child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
  744. features);
  745. if (IS_ERR(child))
  746. return PTR_ERR(child);
  747. child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
  748. features);
  749. if (IS_ERR(child))
  750. return PTR_ERR(child);
  751. child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
  752. features);
  753. if (IS_ERR(child))
  754. return PTR_ERR(child);
  755. child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
  756. features);
  757. if (IS_ERR(child))
  758. return PTR_ERR(child);
  759. child = add_regulator((features & TWL4030_VAUX2)
  760. ? TWL4030_REG_VAUX2_4030
  761. : TWL4030_REG_VAUX2,
  762. pdata->vaux2, features);
  763. if (IS_ERR(child))
  764. return PTR_ERR(child);
  765. child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
  766. features);
  767. if (IS_ERR(child))
  768. return PTR_ERR(child);
  769. child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
  770. features);
  771. if (IS_ERR(child))
  772. return PTR_ERR(child);
  773. child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
  774. features);
  775. if (IS_ERR(child))
  776. return PTR_ERR(child);
  777. }
  778. /* maybe add LDOs that are omitted on cost-reduced parts */
  779. if (twl_has_regulator() && !(features & TPS_SUBSET)
  780. && twl_class_is_4030()) {
  781. child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
  782. features);
  783. if (IS_ERR(child))
  784. return PTR_ERR(child);
  785. child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
  786. features);
  787. if (IS_ERR(child))
  788. return PTR_ERR(child);
  789. child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
  790. features);
  791. if (IS_ERR(child))
  792. return PTR_ERR(child);
  793. child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
  794. features);
  795. if (IS_ERR(child))
  796. return PTR_ERR(child);
  797. child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
  798. features);
  799. if (IS_ERR(child))
  800. return PTR_ERR(child);
  801. child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
  802. features);
  803. if (IS_ERR(child))
  804. return PTR_ERR(child);
  805. }
  806. /* twl6030 regulators */
  807. if (twl_has_regulator() && twl_class_is_6030() &&
  808. !(features & TWL6025_SUBCLASS)) {
  809. child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
  810. features);
  811. if (IS_ERR(child))
  812. return PTR_ERR(child);
  813. child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
  814. features);
  815. if (IS_ERR(child))
  816. return PTR_ERR(child);
  817. child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
  818. features);
  819. if (IS_ERR(child))
  820. return PTR_ERR(child);
  821. child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
  822. features);
  823. if (IS_ERR(child))
  824. return PTR_ERR(child);
  825. child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
  826. features);
  827. if (IS_ERR(child))
  828. return PTR_ERR(child);
  829. child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
  830. features);
  831. if (IS_ERR(child))
  832. return PTR_ERR(child);
  833. child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
  834. features);
  835. if (IS_ERR(child))
  836. return PTR_ERR(child);
  837. child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
  838. features);
  839. if (IS_ERR(child))
  840. return PTR_ERR(child);
  841. child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
  842. features);
  843. if (IS_ERR(child))
  844. return PTR_ERR(child);
  845. child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
  846. features);
  847. if (IS_ERR(child))
  848. return PTR_ERR(child);
  849. child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
  850. features);
  851. if (IS_ERR(child))
  852. return PTR_ERR(child);
  853. child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
  854. features);
  855. if (IS_ERR(child))
  856. return PTR_ERR(child);
  857. child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
  858. features);
  859. if (IS_ERR(child))
  860. return PTR_ERR(child);
  861. child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
  862. features);
  863. if (IS_ERR(child))
  864. return PTR_ERR(child);
  865. }
  866. /* 6030 and 6025 share this regulator */
  867. if (twl_has_regulator() && twl_class_is_6030()) {
  868. child = add_regulator(TWL6030_REG_VANA, pdata->vana,
  869. features);
  870. if (IS_ERR(child))
  871. return PTR_ERR(child);
  872. }
  873. /* twl6025 regulators */
  874. if (twl_has_regulator() && twl_class_is_6030() &&
  875. (features & TWL6025_SUBCLASS)) {
  876. child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
  877. features);
  878. if (IS_ERR(child))
  879. return PTR_ERR(child);
  880. child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
  881. features);
  882. if (IS_ERR(child))
  883. return PTR_ERR(child);
  884. child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
  885. features);
  886. if (IS_ERR(child))
  887. return PTR_ERR(child);
  888. child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
  889. features);
  890. if (IS_ERR(child))
  891. return PTR_ERR(child);
  892. child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
  893. features);
  894. if (IS_ERR(child))
  895. return PTR_ERR(child);
  896. child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
  897. features);
  898. if (IS_ERR(child))
  899. return PTR_ERR(child);
  900. child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
  901. features);
  902. if (IS_ERR(child))
  903. return PTR_ERR(child);
  904. child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
  905. features);
  906. if (IS_ERR(child))
  907. return PTR_ERR(child);
  908. child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
  909. features);
  910. if (IS_ERR(child))
  911. return PTR_ERR(child);
  912. child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
  913. features);
  914. if (IS_ERR(child))
  915. return PTR_ERR(child);
  916. child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
  917. features);
  918. if (IS_ERR(child))
  919. return PTR_ERR(child);
  920. }
  921. if (twl_has_bci() && pdata->bci &&
  922. !(features & (TPS_SUBSET | TWL5031))) {
  923. child = add_child(3, "twl4030_bci",
  924. pdata->bci, sizeof(*pdata->bci), false,
  925. /* irq0 = CHG_PRES, irq1 = BCI */
  926. irq_base + BCI_PRES_INTR_OFFSET,
  927. irq_base + BCI_INTR_OFFSET);
  928. if (IS_ERR(child))
  929. return PTR_ERR(child);
  930. }
  931. return 0;
  932. }
  933. /*----------------------------------------------------------------------*/
  934. /*
  935. * These three functions initialize the on-chip clock framework,
  936. * letting it generate the right frequencies for USB, MADC, and
  937. * other purposes.
  938. */
  939. static inline int __init protect_pm_master(void)
  940. {
  941. int e = 0;
  942. e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
  943. TWL4030_PM_MASTER_PROTECT_KEY);
  944. return e;
  945. }
  946. static inline int __init unprotect_pm_master(void)
  947. {
  948. int e = 0;
  949. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  950. TWL4030_PM_MASTER_KEY_CFG1,
  951. TWL4030_PM_MASTER_PROTECT_KEY);
  952. e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  953. TWL4030_PM_MASTER_KEY_CFG2,
  954. TWL4030_PM_MASTER_PROTECT_KEY);
  955. return e;
  956. }
  957. static void clocks_init(struct device *dev,
  958. struct twl4030_clock_init_data *clock)
  959. {
  960. int e = 0;
  961. struct clk *osc;
  962. u32 rate;
  963. u8 ctrl = HFCLK_FREQ_26_MHZ;
  964. #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
  965. if (cpu_is_omap2430())
  966. osc = clk_get(dev, "osc_ck");
  967. else
  968. osc = clk_get(dev, "osc_sys_ck");
  969. if (IS_ERR(osc)) {
  970. printk(KERN_WARNING "Skipping twl internal clock init and "
  971. "using bootloader value (unknown osc rate)\n");
  972. return;
  973. }
  974. rate = clk_get_rate(osc);
  975. clk_put(osc);
  976. #else
  977. /* REVISIT for non-OMAP systems, pass the clock rate from
  978. * board init code, using platform_data.
  979. */
  980. osc = ERR_PTR(-EIO);
  981. printk(KERN_WARNING "Skipping twl internal clock init and "
  982. "using bootloader value (unknown osc rate)\n");
  983. return;
  984. #endif
  985. switch (rate) {
  986. case 19200000:
  987. ctrl = HFCLK_FREQ_19p2_MHZ;
  988. break;
  989. case 26000000:
  990. ctrl = HFCLK_FREQ_26_MHZ;
  991. break;
  992. case 38400000:
  993. ctrl = HFCLK_FREQ_38p4_MHZ;
  994. break;
  995. }
  996. ctrl |= HIGH_PERF_SQ;
  997. if (clock && clock->ck32k_lowpwr_enable)
  998. ctrl |= CK32K_LOWPWR_EN;
  999. e |= unprotect_pm_master();
  1000. /* effect->MADC+USB ck en */
  1001. e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
  1002. e |= protect_pm_master();
  1003. if (e < 0)
  1004. pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
  1005. }
  1006. /*----------------------------------------------------------------------*/
  1007. static int twl_remove(struct i2c_client *client)
  1008. {
  1009. unsigned i, num_slaves;
  1010. int status;
  1011. if (twl_class_is_4030()) {
  1012. status = twl4030_exit_irq();
  1013. num_slaves = TWL_NUM_SLAVES;
  1014. } else {
  1015. status = twl6030_exit_irq();
  1016. num_slaves = TWL_NUM_SLAVES - 1;
  1017. }
  1018. if (status < 0)
  1019. return status;
  1020. for (i = 0; i < num_slaves; i++) {
  1021. struct twl_client *twl = &twl_modules[i];
  1022. if (twl->client && twl->client != client)
  1023. i2c_unregister_device(twl->client);
  1024. twl_modules[i].client = NULL;
  1025. }
  1026. inuse = false;
  1027. return 0;
  1028. }
  1029. /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
  1030. static int __devinit
  1031. twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
  1032. {
  1033. struct twl4030_platform_data *pdata = client->dev.platform_data;
  1034. struct device_node *node = client->dev.of_node;
  1035. int irq_base = 0;
  1036. int status;
  1037. unsigned i, num_slaves;
  1038. if (node && !pdata) {
  1039. /*
  1040. * XXX: Temporary pdata until the information is correctly
  1041. * retrieved by every TWL modules from DT.
  1042. */
  1043. pdata = devm_kzalloc(&client->dev,
  1044. sizeof(struct twl4030_platform_data),
  1045. GFP_KERNEL);
  1046. if (!pdata)
  1047. return -ENOMEM;
  1048. }
  1049. if (!pdata) {
  1050. dev_dbg(&client->dev, "no platform data?\n");
  1051. return -EINVAL;
  1052. }
  1053. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  1054. dev_dbg(&client->dev, "can't talk I2C?\n");
  1055. return -EIO;
  1056. }
  1057. if (inuse) {
  1058. dev_dbg(&client->dev, "driver is already in use\n");
  1059. return -EBUSY;
  1060. }
  1061. if ((id->driver_data) & TWL6030_CLASS) {
  1062. twl_id = TWL6030_CLASS_ID;
  1063. twl_map = &twl6030_map[0];
  1064. num_slaves = TWL_NUM_SLAVES - 1;
  1065. } else {
  1066. twl_id = TWL4030_CLASS_ID;
  1067. twl_map = &twl4030_map[0];
  1068. num_slaves = TWL_NUM_SLAVES;
  1069. }
  1070. for (i = 0; i < num_slaves; i++) {
  1071. struct twl_client *twl = &twl_modules[i];
  1072. twl->address = client->addr + i;
  1073. if (i == 0) {
  1074. twl->client = client;
  1075. } else {
  1076. twl->client = i2c_new_dummy(client->adapter,
  1077. twl->address);
  1078. if (!twl->client) {
  1079. dev_err(&client->dev,
  1080. "can't attach client %d\n", i);
  1081. status = -ENOMEM;
  1082. goto fail;
  1083. }
  1084. }
  1085. mutex_init(&twl->xfer_lock);
  1086. }
  1087. inuse = true;
  1088. /* setup clock framework */
  1089. clocks_init(&client->dev, pdata->clock);
  1090. /* read TWL IDCODE Register */
  1091. if (twl_id == TWL4030_CLASS_ID) {
  1092. status = twl_read_idcode_register();
  1093. WARN(status < 0, "Error: reading twl_idcode register value\n");
  1094. }
  1095. /* load power event scripts */
  1096. if (twl_has_power() && pdata->power)
  1097. twl4030_power_init(pdata->power);
  1098. /* Maybe init the T2 Interrupt subsystem */
  1099. if (client->irq) {
  1100. if (twl_class_is_4030()) {
  1101. twl4030_init_chip_irq(id->name);
  1102. irq_base = twl4030_init_irq(&client->dev, client->irq);
  1103. } else {
  1104. irq_base = twl6030_init_irq(&client->dev, client->irq);
  1105. }
  1106. if (irq_base < 0) {
  1107. status = irq_base;
  1108. goto fail;
  1109. }
  1110. }
  1111. /*
  1112. * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
  1113. * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
  1114. * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
  1115. */
  1116. if (twl_class_is_4030()) {
  1117. u8 temp;
  1118. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
  1119. temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
  1120. I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
  1121. twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
  1122. }
  1123. status = -ENODEV;
  1124. if (node)
  1125. status = of_platform_populate(node, NULL, NULL, &client->dev);
  1126. if (status)
  1127. status = add_children(pdata, irq_base, id->driver_data);
  1128. fail:
  1129. if (status < 0)
  1130. twl_remove(client);
  1131. return status;
  1132. }
  1133. static const struct i2c_device_id twl_ids[] = {
  1134. { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
  1135. { "twl5030", 0 }, /* T2 updated */
  1136. { "twl5031", TWL5031 }, /* TWL5030 updated */
  1137. { "tps65950", 0 }, /* catalog version of twl5030 */
  1138. { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
  1139. { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
  1140. { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
  1141. and vibrator. Charger in USB module*/
  1142. { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
  1143. { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
  1144. { /* end of list */ },
  1145. };
  1146. MODULE_DEVICE_TABLE(i2c, twl_ids);
  1147. /* One Client Driver , 4 Clients */
  1148. static struct i2c_driver twl_driver = {
  1149. .driver.name = DRIVER_NAME,
  1150. .id_table = twl_ids,
  1151. .probe = twl_probe,
  1152. .remove = twl_remove,
  1153. };
  1154. static int __init twl_init(void)
  1155. {
  1156. return i2c_add_driver(&twl_driver);
  1157. }
  1158. subsys_initcall(twl_init);
  1159. static void __exit twl_exit(void)
  1160. {
  1161. i2c_del_driver(&twl_driver);
  1162. }
  1163. module_exit(twl_exit);
  1164. MODULE_AUTHOR("Texas Instruments, Inc.");
  1165. MODULE_DESCRIPTION("I2C Core interface for TWL");
  1166. MODULE_LICENSE("GPL");