twl-core.c 36 KB

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