twl-core.c 28 KB

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