qpnp-bsi.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/atomic.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spmi.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/bif/driver.h>
  26. #include <linux/qpnp/qpnp-adc.h>
  27. enum qpnp_bsi_irq {
  28. QPNP_BSI_IRQ_ERR,
  29. QPNP_BSI_IRQ_RX,
  30. QPNP_BSI_IRQ_TX,
  31. QPNP_BSI_IRQ_COUNT,
  32. };
  33. enum qpnp_bsi_com_mode {
  34. QPNP_BSI_COM_MODE_IRQ,
  35. QPNP_BSI_COM_MODE_POLL,
  36. };
  37. struct qpnp_bsi_chip {
  38. struct bif_ctrl_desc bdesc;
  39. struct spmi_device *spmi_dev;
  40. struct bif_ctrl_dev *bdev;
  41. struct work_struct slave_irq_work;
  42. u16 base_addr;
  43. u16 batt_id_stat_addr;
  44. int r_pullup_ohm;
  45. int vid_ref_uV;
  46. int tau_index;
  47. int tau_sampling_mask;
  48. enum bif_bus_state state;
  49. enum qpnp_bsi_com_mode com_mode;
  50. int irq[QPNP_BSI_IRQ_COUNT];
  51. atomic_t irq_flag[QPNP_BSI_IRQ_COUNT];
  52. int batt_present_irq;
  53. enum qpnp_vadc_channels batt_id_adc_channel;
  54. struct qpnp_vadc_chip *vadc_dev;
  55. };
  56. #define QPNP_BSI_DRIVER_NAME "qcom,qpnp-bsi"
  57. enum qpnp_bsi_registers {
  58. QPNP_BSI_REG_TYPE = 0x04,
  59. QPNP_BSI_REG_SUBTYPE = 0x05,
  60. QPNP_BSI_REG_STATUS = 0x08,
  61. QPNP_BSI_REG_ENABLE = 0x46,
  62. QPNP_BSI_REG_CLEAR_ERROR = 0x4F,
  63. QPNP_BSI_REG_FORCE_BCL_LOW = 0x51,
  64. QPNP_BSI_REG_TAU_CONFIG = 0x52,
  65. QPNP_BSI_REG_MODE = 0x53,
  66. QPNP_BSI_REG_RX_TX_ENABLE = 0x54,
  67. QPNP_BSI_REG_TX_DATA_LOW = 0x5A,
  68. QPNP_BSI_REG_TX_DATA_HIGH = 0x5B,
  69. QPNP_BSI_REG_TX_CTRL = 0x5D,
  70. QPNP_BSI_REG_RX_DATA_LOW = 0x60,
  71. QPNP_BSI_REG_RX_DATA_HIGH = 0x61,
  72. QPNP_BSI_REG_RX_SOURCE = 0x62,
  73. QPNP_BSI_REG_BSI_ERROR = 0x70,
  74. };
  75. #define QPNP_BSI_TYPE 0x02
  76. #define QPNP_BSI_SUBTYPE 0x10
  77. #define QPNP_BSI_STATUS_ERROR 0x10
  78. #define QPNP_BSI_STATUS_TX_BUSY 0x08
  79. #define QPNP_BSI_STATUS_RX_BUSY 0x04
  80. #define QPNP_BSI_STATUS_TX_GO_BUSY 0x02
  81. #define QPNP_BSI_STATUS_RX_DATA_READY 0x01
  82. #define QPNP_BSI_ENABLE_MASK 0x80
  83. #define QPNP_BSI_ENABLE 0x80
  84. #define QPNP_BSI_DISABLE 0x00
  85. #define QPNP_BSI_TAU_CONFIG_SAMPLE_MASK 0x10
  86. #define QPNP_BSI_TAU_CONFIG_SAMPLE_8X 0x10
  87. #define QPNP_BSI_TAU_CONFIG_SAMPLE_4X 0x00
  88. #define QPNP_BSI_TAU_CONFIG_SPEED_MASK 0x07
  89. #define QPNP_BSI_MODE_TX_PULSE_MASK 0x10
  90. #define QPNP_BSI_MODE_TX_PULSE_INT 0x10
  91. #define QPNP_BSI_MODE_TX_PULSE_DATA 0x00
  92. #define QPNP_BSI_MODE_RX_PULSE_MASK 0x08
  93. #define QPNP_BSI_MODE_RX_PULSE_INT 0x08
  94. #define QPNP_BSI_MODE_RX_PULSE_DATA 0x00
  95. #define QPNP_BSI_MODE_TX_PULSE_T_MASK 0x04
  96. #define QPNP_BSI_MODE_TX_PULSE_T_WAKE 0x04
  97. #define QPNP_BSI_MODE_TX_PULSE_T_1_TAU 0x00
  98. #define QPNP_BSI_MODE_RX_FORMAT_MASK 0x02
  99. #define QPNP_BSI_MODE_RX_FORMAT_17_BIT 0x02
  100. #define QPNP_BSI_MODE_RX_FORMAT_11_BIT 0x00
  101. #define QPNP_BSI_MODE_TX_FORMAT_MASK 0x01
  102. #define QPNP_BSI_MODE_TX_FORMAT_17_BIT 0x01
  103. #define QPNP_BSI_MODE_TX_FORMAT_11_BIT 0x00
  104. #define QPNP_BSI_TX_ENABLE_MASK 0x80
  105. #define QPNP_BSI_TX_ENABLE 0x80
  106. #define QPNP_BSI_TX_DISABLE 0x00
  107. #define QPNP_BSI_RX_ENABLE_MASK 0x40
  108. #define QPNP_BSI_RX_ENABLE 0x40
  109. #define QPNP_BSI_RX_DISABLE 0x00
  110. #define QPNP_BSI_TX_DATA_HIGH_MASK 0x07
  111. #define QPNP_BSI_TX_CTRL_GO 0x01
  112. #define QPNP_BSI_RX_DATA_HIGH_MASK 0x07
  113. #define QPNP_BSI_RX_SRC_LOOPBACK_FLAG 0x10
  114. #define QPNP_BSI_BSI_ERROR_CLEAR 0x80
  115. #define QPNP_SMBB_BAT_IF_BATT_PRES_MASK 0x80
  116. #define QPNP_SMBB_BAT_IF_BATT_ID_MASK 0x01
  117. #define QPNP_BSI_NUM_CLOCK_PERIODS 8
  118. struct qpnp_bsi_tau {
  119. int period_4x_ns[QPNP_BSI_NUM_CLOCK_PERIODS];
  120. int period_8x_ns[QPNP_BSI_NUM_CLOCK_PERIODS];
  121. int period_4x_us[QPNP_BSI_NUM_CLOCK_PERIODS];
  122. int period_8x_us[QPNP_BSI_NUM_CLOCK_PERIODS];
  123. };
  124. /* Tau BIF clock periods in ns supported by BSI for either 4x or 8x sampling. */
  125. static const struct qpnp_bsi_tau qpnp_bsi_tau_period = {
  126. .period_4x_ns = {
  127. 150420, 122080, 61040, 31670, 15830, 7920, 3960, 2080
  128. },
  129. .period_8x_ns = {
  130. 150420, 122080, 63330, 31670, 15830, 7920, 4170, 2080
  131. },
  132. .period_4x_us = {
  133. 151, 122, 61, 32, 16, 8, 4, 2
  134. },
  135. .period_8x_us = {
  136. 151, 122, 64, 32, 16, 8, 4, 2
  137. },
  138. };
  139. #define QPNP_BSI_MIN_CLOCK_SPEED_NS 2080
  140. #define QPNP_BSI_MAX_CLOCK_SPEED_NS 150420
  141. #define QPNP_BSI_MIN_PULLUP_OHM 1000
  142. #define QPNP_BSI_MAX_PULLUP_OHM 500000
  143. #define QPNP_BSI_DEFAULT_PULLUP_OHM 100000
  144. #define QPNP_BSI_MIN_VID_REF_UV 500000
  145. #define QPNP_BSI_MAX_VID_REF_UV 5000000
  146. #define QPNP_BSI_DEFAULT_VID_REF_UV 1800000
  147. /* These have units of tau_bif. */
  148. #define QPNP_BSI_MAX_TRANSMIT_CYCLES 46
  149. #define QPNP_BSI_MIN_RECEIVE_CYCLES 24
  150. #define QPNP_BSI_MAX_BUS_QUERY_CYCLES 17
  151. /*
  152. * Maximum time in microseconds for a slave to transition from suspend to active
  153. * state.
  154. */
  155. #define QPNP_BSI_MAX_SLAVE_ACTIVIATION_DELAY_US 50
  156. /*
  157. * Maximum time in milliseconds for a slave to transition from power down to
  158. * active state.
  159. */
  160. #define QPNP_BSI_MAX_SLAVE_POWER_UP_DELAY_MS 10
  161. #define QPNP_BSI_POWER_UP_LOW_DELAY_US 240
  162. /*
  163. * Latencies that are used when determining if polling or interrupts should be
  164. * used for a given transaction.
  165. */
  166. #define QPNP_BSI_MAX_IRQ_LATENCY_US 170
  167. #define QPNP_BSI_MAX_BSI_DATA_READ_LATENCY_US 16
  168. static int qpnp_bsi_set_bus_state(struct bif_ctrl_dev *bdev, int state);
  169. static inline int qpnp_bsi_read(struct qpnp_bsi_chip *chip, u16 addr, u8 *buf,
  170. int len)
  171. {
  172. int rc;
  173. rc = spmi_ext_register_readl(chip->spmi_dev->ctrl,
  174. chip->spmi_dev->sid, chip->base_addr + addr, buf, len);
  175. if (rc)
  176. dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_readl() failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
  177. __func__, chip->spmi_dev->sid, chip->base_addr + addr,
  178. len, rc);
  179. return rc;
  180. }
  181. static inline int qpnp_bsi_write(struct qpnp_bsi_chip *chip, u16 addr, u8 *buf,
  182. int len)
  183. {
  184. int rc;
  185. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl,
  186. chip->spmi_dev->sid, chip->base_addr + addr, buf, len);
  187. if (rc)
  188. dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_writel() failed. sid=%d, addr=%04X, len=%d, rc=%d\n",
  189. __func__, chip->spmi_dev->sid, chip->base_addr + addr,
  190. len, rc);
  191. return rc;
  192. }
  193. enum qpnp_bsi_rx_tx_state {
  194. QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF,
  195. QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA,
  196. QPNP_BSI_RX_TX_STATE_RX_OFF_TX_INT,
  197. QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA,
  198. QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA,
  199. QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF,
  200. };
  201. static int qpnp_bsi_rx_tx_config(struct qpnp_bsi_chip *chip,
  202. enum qpnp_bsi_rx_tx_state state)
  203. {
  204. u8 buf[2] = {0, 0};
  205. int rc;
  206. buf[0] = QPNP_BSI_MODE_TX_FORMAT_11_BIT
  207. | QPNP_BSI_MODE_RX_FORMAT_11_BIT;
  208. switch (state) {
  209. case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF:
  210. buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
  211. QPNP_BSI_MODE_RX_PULSE_DATA;
  212. buf[1] = QPNP_BSI_TX_DISABLE | QPNP_BSI_RX_DISABLE;
  213. break;
  214. case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA:
  215. buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
  216. QPNP_BSI_MODE_RX_PULSE_DATA;
  217. buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
  218. break;
  219. case QPNP_BSI_RX_TX_STATE_RX_OFF_TX_INT:
  220. buf[0] |= QPNP_BSI_MODE_TX_PULSE_INT |
  221. QPNP_BSI_MODE_RX_PULSE_DATA;
  222. buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
  223. break;
  224. case QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA:
  225. buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
  226. QPNP_BSI_MODE_RX_PULSE_INT;
  227. buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_ENABLE;
  228. break;
  229. case QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA:
  230. buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
  231. QPNP_BSI_MODE_RX_PULSE_DATA;
  232. buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_ENABLE;
  233. break;
  234. case QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF:
  235. buf[0] |= QPNP_BSI_MODE_TX_PULSE_DATA |
  236. QPNP_BSI_MODE_RX_PULSE_INT;
  237. buf[1] = QPNP_BSI_TX_DISABLE | QPNP_BSI_RX_DISABLE;
  238. break;
  239. default:
  240. dev_err(&chip->spmi_dev->dev, "%s: invalid state=%d\n",
  241. __func__, state);
  242. return -EINVAL;
  243. }
  244. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_MODE, buf, 2);
  245. if (rc)
  246. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  247. __func__, rc);
  248. return rc;
  249. }
  250. static void qpnp_bsi_slave_irq_work(struct work_struct *work)
  251. {
  252. struct qpnp_bsi_chip *chip
  253. = container_of(work, struct qpnp_bsi_chip, slave_irq_work);
  254. int rc;
  255. rc = bif_ctrl_notify_slave_irq(chip->bdev);
  256. if (rc)
  257. pr_err("Could not notify BIF core about slave interrupt, rc=%d\n",
  258. rc);
  259. }
  260. static irqreturn_t qpnp_bsi_isr(int irq, void *data)
  261. {
  262. struct qpnp_bsi_chip *chip = data;
  263. bool found = false;
  264. int i;
  265. for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++) {
  266. if (irq == chip->irq[i]) {
  267. found = true;
  268. atomic_cmpxchg(&chip->irq_flag[i], 0, 1);
  269. /* Check if this is a slave interrupt. */
  270. if (i == QPNP_BSI_IRQ_RX
  271. && chip->state == BIF_BUS_STATE_INTERRUPT) {
  272. /* Slave IRQ makes the bus active. */
  273. qpnp_bsi_rx_tx_config(chip,
  274. QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  275. chip->state = BIF_BUS_STATE_ACTIVE;
  276. schedule_work(&chip->slave_irq_work);
  277. }
  278. }
  279. }
  280. if (!found)
  281. pr_err("Unknown interrupt: %d\n", irq);
  282. return IRQ_HANDLED;
  283. }
  284. static irqreturn_t qpnp_bsi_batt_present_isr(int irq, void *data)
  285. {
  286. struct qpnp_bsi_chip *chip = data;
  287. int rc;
  288. if (!chip->bdev)
  289. return IRQ_HANDLED;
  290. rc = bif_ctrl_notify_battery_changed(chip->bdev);
  291. if (rc)
  292. pr_err("Could not notify about battery state change, rc=%d\n",
  293. rc);
  294. return IRQ_HANDLED;
  295. }
  296. static void qpnp_bsi_set_com_mode(struct qpnp_bsi_chip *chip,
  297. enum qpnp_bsi_com_mode mode)
  298. {
  299. int i;
  300. if (chip->com_mode == mode)
  301. return;
  302. if (mode == QPNP_BSI_COM_MODE_IRQ)
  303. for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
  304. enable_irq(chip->irq[i]);
  305. else
  306. for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
  307. disable_irq(chip->irq[i]);
  308. chip->com_mode = mode;
  309. }
  310. static inline bool qpnp_bsi_check_irq(struct qpnp_bsi_chip *chip, int irq)
  311. {
  312. return atomic_cmpxchg(&chip->irq_flag[irq], 1, 0);
  313. }
  314. static void qpnp_bsi_clear_irq_flags(struct qpnp_bsi_chip *chip)
  315. {
  316. int i;
  317. for (i = 0; i < QPNP_BSI_IRQ_COUNT; i++)
  318. atomic_set(&chip->irq_flag[i], 0);
  319. }
  320. static inline int qpnp_bsi_get_tau_ns(struct qpnp_bsi_chip *chip)
  321. {
  322. if (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X)
  323. return qpnp_bsi_tau_period.period_4x_ns[chip->tau_index];
  324. else
  325. return qpnp_bsi_tau_period.period_8x_ns[chip->tau_index];
  326. }
  327. static inline int qpnp_bsi_get_tau_us(struct qpnp_bsi_chip *chip)
  328. {
  329. if (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X)
  330. return qpnp_bsi_tau_period.period_4x_us[chip->tau_index];
  331. else
  332. return qpnp_bsi_tau_period.period_8x_us[chip->tau_index];
  333. }
  334. /* Checks if BSI is in an error state and clears the error if it is. */
  335. static int qpnp_bsi_clear_bsi_error(struct qpnp_bsi_chip *chip)
  336. {
  337. int rc, delay_us;
  338. u8 reg;
  339. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_BSI_ERROR, &reg, 1);
  340. if (rc) {
  341. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
  342. __func__, rc);
  343. return rc;
  344. }
  345. if (reg > 0) {
  346. /*
  347. * Delay before clearing the BSI error in case a transaction is
  348. * still in flight.
  349. */
  350. delay_us = QPNP_BSI_MAX_TRANSMIT_CYCLES
  351. * qpnp_bsi_get_tau_us(chip);
  352. udelay(delay_us);
  353. pr_info("PMIC BSI module in error state, error=%d\n", reg);
  354. reg = QPNP_BSI_BSI_ERROR_CLEAR;
  355. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_CLEAR_ERROR, &reg, 1);
  356. if (rc)
  357. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  358. __func__, rc);
  359. }
  360. return rc;
  361. }
  362. static int qpnp_bsi_get_bsi_error(struct qpnp_bsi_chip *chip)
  363. {
  364. int rc;
  365. u8 reg;
  366. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_BSI_ERROR, &reg, 1);
  367. if (rc) {
  368. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
  369. __func__, rc);
  370. return rc;
  371. }
  372. return reg;
  373. }
  374. static int qpnp_bsi_wait_for_tx(struct qpnp_bsi_chip *chip, int timeout)
  375. {
  376. int rc = 0;
  377. /* Wait for TX or ERR IRQ. */
  378. while (timeout > 0) {
  379. if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_ERR)) {
  380. dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
  381. __func__, qpnp_bsi_get_bsi_error(chip));
  382. return -EIO;
  383. }
  384. if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_TX))
  385. break;
  386. udelay(1);
  387. timeout--;
  388. }
  389. if (timeout == 0) {
  390. rc = -ETIMEDOUT;
  391. dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, no interrupts received, rc=%d\n",
  392. __func__, rc);
  393. return rc;
  394. }
  395. return rc;
  396. }
  397. static int qpnp_bsi_issue_transaction(struct qpnp_bsi_chip *chip,
  398. int transaction, u8 data)
  399. {
  400. int rc;
  401. u8 buf[4];
  402. /* MIPI_BIF_DATA_TX_0 = BIF word bits 7 to 0 */
  403. buf[0] = data;
  404. /* MIPI_BIF_DATA_TX_1 = BIF word BCF, bits 9 to 8 */
  405. buf[1] = transaction & QPNP_BSI_TX_DATA_HIGH_MASK;
  406. /* MIPI_BIF_DATA_TX_2 ignored */
  407. buf[2] = 0x00;
  408. /* MIPI_BIF_TX_CTL bit 0 written to start the transaction. */
  409. buf[3] = QPNP_BSI_TX_CTRL_GO;
  410. /* Write the TX_DATA bytes and initiate the transaction. */
  411. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TX_DATA_LOW, buf, 4);
  412. if (rc)
  413. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  414. __func__, rc);
  415. return rc;
  416. }
  417. static int qpnp_bsi_issue_transaction_wait_for_tx(struct qpnp_bsi_chip *chip,
  418. int transaction, u8 data)
  419. {
  420. int rc, timeout;
  421. rc = qpnp_bsi_issue_transaction(chip, transaction, data);
  422. if (rc)
  423. return rc;
  424. timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip)
  425. + QPNP_BSI_MAX_IRQ_LATENCY_US;
  426. rc = qpnp_bsi_wait_for_tx(chip, timeout);
  427. return rc;
  428. }
  429. static int qpnp_bsi_wait_for_rx(struct qpnp_bsi_chip *chip, int timeout)
  430. {
  431. int rc = 0;
  432. /* Wait for RX IRQ to indicate that data is ready to read. */
  433. while (timeout > 0) {
  434. if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_ERR)) {
  435. dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
  436. __func__, qpnp_bsi_get_bsi_error(chip));
  437. return -EIO;
  438. }
  439. if (qpnp_bsi_check_irq(chip, QPNP_BSI_IRQ_RX))
  440. break;
  441. udelay(1);
  442. timeout--;
  443. }
  444. if (timeout == 0)
  445. rc = -ETIMEDOUT;
  446. return rc;
  447. }
  448. static int qpnp_bsi_bus_transaction(struct bif_ctrl_dev *bdev, int transaction,
  449. u8 data)
  450. {
  451. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  452. int rc;
  453. qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
  454. rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
  455. if (rc) {
  456. dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
  457. __func__, rc);
  458. return rc;
  459. }
  460. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA);
  461. if (rc)
  462. return rc;
  463. rc = qpnp_bsi_clear_bsi_error(chip);
  464. if (rc)
  465. return rc;
  466. qpnp_bsi_clear_irq_flags(chip);
  467. rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
  468. if (rc)
  469. return rc;
  470. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  471. return rc;
  472. }
  473. static int qpnp_bsi_bus_transaction_query(struct bif_ctrl_dev *bdev,
  474. int transaction, u8 data, bool *query_response)
  475. {
  476. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  477. int rc, timeout;
  478. qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
  479. rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
  480. if (rc) {
  481. dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
  482. __func__, rc);
  483. return rc;
  484. }
  485. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA);
  486. if (rc)
  487. return rc;
  488. rc = qpnp_bsi_clear_bsi_error(chip);
  489. if (rc)
  490. return rc;
  491. qpnp_bsi_clear_irq_flags(chip);
  492. rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
  493. if (rc)
  494. return rc;
  495. timeout = QPNP_BSI_MAX_BUS_QUERY_CYCLES * qpnp_bsi_get_tau_us(chip)
  496. + QPNP_BSI_MAX_IRQ_LATENCY_US;
  497. rc = qpnp_bsi_wait_for_rx(chip, timeout);
  498. if (rc == 0) {
  499. *query_response = true;
  500. } else if (rc == -ETIMEDOUT) {
  501. *query_response = false;
  502. rc = 0;
  503. }
  504. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  505. return rc;
  506. }
  507. static int qpnp_bsi_bus_transaction_read(struct bif_ctrl_dev *bdev,
  508. int transaction, u8 data, int *response)
  509. {
  510. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  511. int rc, timeout;
  512. u8 buf[3];
  513. qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
  514. rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
  515. if (rc) {
  516. dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
  517. __func__, rc);
  518. return rc;
  519. }
  520. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA);
  521. if (rc)
  522. return rc;
  523. rc = qpnp_bsi_clear_bsi_error(chip);
  524. if (rc)
  525. return rc;
  526. qpnp_bsi_clear_irq_flags(chip);
  527. rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, transaction, data);
  528. if (rc)
  529. return rc;
  530. timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip)
  531. + QPNP_BSI_MAX_IRQ_LATENCY_US;
  532. rc = qpnp_bsi_wait_for_rx(chip, timeout);
  533. if (rc) {
  534. if (rc == -ETIMEDOUT) {
  535. /*
  536. * No error message is printed in this case in order
  537. * to provide silent operation when checking if a slave
  538. * is selected using the transaction query bus command.
  539. */
  540. dev_dbg(&chip->spmi_dev->dev, "%s: transaction timed out, no interrupts received, rc=%d\n",
  541. __func__, rc);
  542. }
  543. return rc;
  544. }
  545. /* Read the RX_DATA bytes. */
  546. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_RX_DATA_LOW, buf, 3);
  547. if (rc) {
  548. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
  549. __func__, rc);
  550. return rc;
  551. }
  552. if (buf[2] & QPNP_BSI_RX_SRC_LOOPBACK_FLAG) {
  553. rc = -EIO;
  554. dev_err(&chip->spmi_dev->dev, "%s: unexpected loopback data read, rc=%d\n",
  555. __func__, rc);
  556. return rc;
  557. }
  558. *response = ((int)(buf[1] & QPNP_BSI_RX_DATA_HIGH_MASK) << 8) | buf[0];
  559. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  560. return 0;
  561. }
  562. /*
  563. * Wait for RX_FLOW_STATUS to be set to 1 which indicates that another BIF word
  564. * can be read from PMIC registers.
  565. */
  566. static int qpnp_bsi_wait_for_rx_data(struct qpnp_bsi_chip *chip)
  567. {
  568. int rc = 0;
  569. int timeout;
  570. u8 reg;
  571. timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);
  572. /* Wait for RX_FLOW_STATUS == 1 or ERR_FLAG == 1. */
  573. while (timeout > 0) {
  574. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
  575. if (rc) {
  576. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  577. __func__, rc);
  578. return rc;
  579. }
  580. if (reg & QPNP_BSI_STATUS_ERROR) {
  581. dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
  582. __func__, qpnp_bsi_get_bsi_error(chip));
  583. return -EIO;
  584. }
  585. if (reg & QPNP_BSI_STATUS_RX_DATA_READY) {
  586. /* BSI RX has data word latched. */
  587. return 0;
  588. }
  589. udelay(1);
  590. timeout--;
  591. }
  592. rc = -ETIMEDOUT;
  593. dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, RX_FLOW_STATUS never set to 1, rc=%d\n",
  594. __func__, rc);
  595. return rc;
  596. }
  597. /*
  598. * Wait for TX_GO_STATUS to be set to 0 which indicates that another BIF word
  599. * can be enqueued.
  600. */
  601. static int qpnp_bsi_wait_for_tx_go(struct qpnp_bsi_chip *chip)
  602. {
  603. int rc = 0;
  604. int timeout;
  605. u8 reg;
  606. timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);
  607. /* Wait for TX_GO_STATUS == 0 or ERR_FLAG == 1. */
  608. while (timeout > 0) {
  609. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
  610. if (rc) {
  611. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  612. __func__, rc);
  613. return rc;
  614. }
  615. if (reg & QPNP_BSI_STATUS_ERROR) {
  616. dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
  617. __func__, qpnp_bsi_get_bsi_error(chip));
  618. return -EIO;
  619. }
  620. if (!(reg & QPNP_BSI_STATUS_TX_GO_BUSY)) {
  621. /* BSI TX is ready to accept the next word. */
  622. return 0;
  623. }
  624. udelay(1);
  625. timeout--;
  626. }
  627. rc = -ETIMEDOUT;
  628. dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, TX_GO_STATUS never set to 0, rc=%d\n",
  629. __func__, rc);
  630. return rc;
  631. }
  632. /*
  633. * Wait for TX_BUSY to be set to 0 which indicates that the TX data has been
  634. * successfully transmitted.
  635. */
  636. static int qpnp_bsi_wait_for_tx_idle(struct qpnp_bsi_chip *chip)
  637. {
  638. int rc = 0;
  639. int timeout;
  640. u8 reg;
  641. timeout = QPNP_BSI_MAX_TRANSMIT_CYCLES * qpnp_bsi_get_tau_us(chip);
  642. /* Wait for TX_BUSY == 0 or ERR_FLAG == 1. */
  643. while (timeout > 0) {
  644. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_STATUS, &reg, 1);
  645. if (rc) {
  646. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  647. __func__, rc);
  648. return rc;
  649. }
  650. if (reg & QPNP_BSI_STATUS_ERROR) {
  651. dev_err(&chip->spmi_dev->dev, "%s: transaction error occurred, BSI error=%d\n",
  652. __func__, qpnp_bsi_get_bsi_error(chip));
  653. return -EIO;
  654. }
  655. if (!(reg & QPNP_BSI_STATUS_TX_BUSY)) {
  656. /* BSI TX is idle. */
  657. return 0;
  658. }
  659. udelay(1);
  660. timeout--;
  661. }
  662. rc = -ETIMEDOUT;
  663. dev_err(&chip->spmi_dev->dev, "%s: transaction timed out, TX_BUSY never set to 0, rc=%d\n",
  664. __func__, rc);
  665. return rc;
  666. }
  667. /*
  668. * For burst read length greater than 1, send necessary RBL and RBE BIF bus
  669. * commands.
  670. */
  671. static int qpnp_bsi_send_burst_length(struct qpnp_bsi_chip *chip, int burst_len)
  672. {
  673. int rc = 0;
  674. /*
  675. * Send burst read length bus commands according to the following:
  676. *
  677. * 1 --> No RBE or RBL
  678. * 2 - 15 = x --> RBLx
  679. * 16 - 255 = 16 * y + x --> RBEy and RBLx (RBL0 not sent)
  680. * 256 --> RBL0
  681. */
  682. if (burst_len == 256) {
  683. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
  684. BIF_CMD_RBL);
  685. if (rc)
  686. return rc;
  687. rc = qpnp_bsi_wait_for_tx_go(chip);
  688. if (rc)
  689. return rc;
  690. } else if (burst_len >= 16) {
  691. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
  692. BIF_CMD_RBE + (burst_len / 16));
  693. if (rc)
  694. return rc;
  695. rc = qpnp_bsi_wait_for_tx_go(chip);
  696. if (rc)
  697. return rc;
  698. }
  699. if (burst_len % 16 && burst_len > 1) {
  700. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_BC,
  701. BIF_CMD_RBL + (burst_len % 16));
  702. if (rc)
  703. return rc;
  704. rc = qpnp_bsi_wait_for_tx_go(chip);
  705. if (rc)
  706. return rc;
  707. }
  708. return rc;
  709. }
  710. /* Perform validation steps on received BIF data. */
  711. static int qpnp_bsi_validate_rx_data(struct qpnp_bsi_chip *chip, int response,
  712. u8 rx2_data, bool last_word)
  713. {
  714. int err = -EIO;
  715. if (rx2_data & QPNP_BSI_RX_SRC_LOOPBACK_FLAG) {
  716. dev_err(&chip->spmi_dev->dev, "%s: unexpected loopback data read, rc=%d\n",
  717. __func__, err);
  718. return err;
  719. }
  720. if (!(response & BIF_SLAVE_RD_ACK)) {
  721. dev_err(&chip->spmi_dev->dev, "%s: BIF register read error=0x%02X\n",
  722. __func__, response & BIF_SLAVE_RD_ERR);
  723. return err;
  724. }
  725. if (last_word && !(response & BIF_SLAVE_RD_EOT)) {
  726. dev_err(&chip->spmi_dev->dev, "%s: BIF register read error, last RD packet has EOT=0\n",
  727. __func__);
  728. return err;
  729. } else if (!last_word && (response & BIF_SLAVE_RD_EOT)) {
  730. dev_err(&chip->spmi_dev->dev, "%s: BIF register read error, RD packet other than last has EOT=1\n",
  731. __func__);
  732. return err;
  733. }
  734. return 0;
  735. }
  736. /* Performs all BIF transactions in order to utilize burst reads. */
  737. static int qpnp_bsi_read_slave_registers(struct bif_ctrl_dev *bdev, u16 addr,
  738. u8 *data, int len)
  739. {
  740. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  741. int response = 0;
  742. unsigned long flags;
  743. int rc, rc2, i, burst_len;
  744. u8 buf[3];
  745. qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_POLL);
  746. rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
  747. if (rc) {
  748. dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
  749. __func__, rc);
  750. return rc;
  751. }
  752. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_DATA_TX_DATA);
  753. if (rc)
  754. return rc;
  755. rc = qpnp_bsi_clear_bsi_error(chip);
  756. if (rc)
  757. return rc;
  758. qpnp_bsi_clear_irq_flags(chip);
  759. while (len > 0) {
  760. burst_len = min(len, 256);
  761. rc = qpnp_bsi_send_burst_length(chip, burst_len);
  762. if (rc)
  763. return rc;
  764. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_ERA, addr >> 8);
  765. if (rc)
  766. return rc;
  767. rc = qpnp_bsi_wait_for_tx_go(chip);
  768. if (rc)
  769. return rc;
  770. /* Perform burst read in atomic context. */
  771. local_irq_save(flags);
  772. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_RRA,
  773. addr & 0xFF);
  774. if (rc)
  775. goto burst_err;
  776. for (i = 0; i < burst_len; i++) {
  777. rc = qpnp_bsi_wait_for_rx_data(chip);
  778. if (rc)
  779. goto burst_err;
  780. /* Read the RX_DATA bytes. */
  781. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_RX_DATA_LOW, buf,
  782. 3);
  783. if (rc) {
  784. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_read() failed, rc=%d\n",
  785. __func__, rc);
  786. goto burst_err;
  787. }
  788. response = ((buf[1] & QPNP_BSI_RX_DATA_HIGH_MASK) << 8)
  789. | buf[0];
  790. rc = qpnp_bsi_validate_rx_data(chip, response, buf[2],
  791. i == burst_len - 1);
  792. if (rc)
  793. goto burst_err;
  794. data[i] = buf[0];
  795. }
  796. local_irq_restore(flags);
  797. addr += burst_len;
  798. data += burst_len;
  799. len -= burst_len;
  800. }
  801. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  802. return rc;
  803. burst_err:
  804. local_irq_restore(flags);
  805. rc2 = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  806. if (rc2 < 0)
  807. rc = rc2;
  808. return rc;
  809. }
  810. /* Performs all BIF transactions in order to utilize burst writes. */
  811. static int qpnp_bsi_write_slave_registers(struct bif_ctrl_dev *bdev, u16 addr,
  812. const u8 *data, int len)
  813. {
  814. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  815. unsigned long flags;
  816. int rc, rc2, i;
  817. qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_POLL);
  818. rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
  819. if (rc) {
  820. dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
  821. __func__, rc);
  822. return rc;
  823. }
  824. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_DATA);
  825. if (rc)
  826. return rc;
  827. rc = qpnp_bsi_clear_bsi_error(chip);
  828. if (rc)
  829. return rc;
  830. qpnp_bsi_clear_irq_flags(chip);
  831. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_ERA, addr >> 8);
  832. if (rc)
  833. return rc;
  834. rc = qpnp_bsi_wait_for_tx_go(chip);
  835. if (rc)
  836. return rc;
  837. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_WRA, addr & 0xFF);
  838. if (rc)
  839. return rc;
  840. rc = qpnp_bsi_wait_for_tx_go(chip);
  841. if (rc)
  842. return rc;
  843. /* Perform burst write in atomic context. */
  844. local_irq_save(flags);
  845. for (i = 0; i < len; i++) {
  846. rc = qpnp_bsi_issue_transaction(chip, BIF_TRANS_WD, data[i]);
  847. if (rc)
  848. goto burst_err;
  849. rc = qpnp_bsi_wait_for_tx_go(chip);
  850. if (rc)
  851. goto burst_err;
  852. }
  853. rc = qpnp_bsi_wait_for_tx_idle(chip);
  854. if (rc)
  855. goto burst_err;
  856. local_irq_restore(flags);
  857. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  858. return rc;
  859. burst_err:
  860. local_irq_restore(flags);
  861. rc2 = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_OFF_TX_OFF);
  862. if (rc2 < 0)
  863. rc = rc2;
  864. return rc;
  865. }
  866. static int qpnp_bsi_bus_set_interrupt_mode(struct bif_ctrl_dev *bdev)
  867. {
  868. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  869. int rc;
  870. qpnp_bsi_set_com_mode(chip, QPNP_BSI_COM_MODE_IRQ);
  871. /*
  872. * Temporarily change the bus to active state so that the EINT command
  873. * can be issued.
  874. */
  875. rc = qpnp_bsi_set_bus_state(bdev, BIF_BUS_STATE_ACTIVE);
  876. if (rc) {
  877. dev_err(&chip->spmi_dev->dev, "%s: failed to set bus state, rc=%d\n",
  878. __func__, rc);
  879. return rc;
  880. }
  881. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_DATA);
  882. if (rc)
  883. return rc;
  884. /*
  885. * Set the bus state to interrupt mode so that an RX interrupt which
  886. * occurs immediately after issuing the EINT command is handled
  887. * properly.
  888. */
  889. chip->state = BIF_BUS_STATE_INTERRUPT;
  890. rc = qpnp_bsi_clear_bsi_error(chip);
  891. if (rc)
  892. return rc;
  893. qpnp_bsi_clear_irq_flags(chip);
  894. /* Send EINT bus command. */
  895. rc = qpnp_bsi_issue_transaction_wait_for_tx(chip, BIF_TRANS_BC,
  896. BIF_CMD_EINT);
  897. if (rc)
  898. return rc;
  899. rc = qpnp_bsi_rx_tx_config(chip, QPNP_BSI_RX_TX_STATE_RX_INT_TX_OFF);
  900. return rc;
  901. }
  902. static int qpnp_bsi_bus_set_active_mode(struct bif_ctrl_dev *bdev,
  903. int prev_state)
  904. {
  905. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  906. int rc;
  907. u8 buf[2];
  908. rc = qpnp_bsi_clear_bsi_error(chip);
  909. if (rc)
  910. return rc;
  911. buf[0] = QPNP_BSI_MODE_TX_PULSE_INT |
  912. QPNP_BSI_MODE_RX_PULSE_DATA;
  913. buf[1] = QPNP_BSI_TX_ENABLE | QPNP_BSI_RX_DISABLE;
  914. if (prev_state == BIF_BUS_STATE_INTERRUPT)
  915. buf[0] |= QPNP_BSI_MODE_TX_PULSE_T_1_TAU;
  916. else
  917. buf[0] |= QPNP_BSI_MODE_TX_PULSE_T_WAKE;
  918. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_MODE, buf, 2);
  919. if (rc) {
  920. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  921. __func__, rc);
  922. return rc;
  923. }
  924. buf[0] = QPNP_BSI_TX_CTRL_GO;
  925. /* Initiate BCL low pulse. */
  926. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TX_CTRL, buf, 1);
  927. if (rc) {
  928. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  929. __func__, rc);
  930. return rc;
  931. }
  932. switch (prev_state) {
  933. case BIF_BUS_STATE_INTERRUPT:
  934. udelay(qpnp_bsi_get_tau_us(chip) * 4);
  935. break;
  936. case BIF_BUS_STATE_STANDBY:
  937. udelay(qpnp_bsi_get_tau_us(chip)
  938. + QPNP_BSI_MAX_SLAVE_ACTIVIATION_DELAY_US
  939. + QPNP_BSI_POWER_UP_LOW_DELAY_US);
  940. break;
  941. case BIF_BUS_STATE_POWER_DOWN:
  942. case BIF_BUS_STATE_MASTER_DISABLED:
  943. msleep(QPNP_BSI_MAX_SLAVE_POWER_UP_DELAY_MS);
  944. break;
  945. }
  946. return rc;
  947. }
  948. static int qpnp_bsi_get_bus_state(struct bif_ctrl_dev *bdev)
  949. {
  950. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  951. return chip->state;
  952. }
  953. static int qpnp_bsi_set_bus_state(struct bif_ctrl_dev *bdev, int state)
  954. {
  955. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  956. int rc = 0;
  957. u8 reg;
  958. if (state == chip->state)
  959. return 0;
  960. if (chip->state == BIF_BUS_STATE_MASTER_DISABLED) {
  961. /*
  962. * Enable the BSI peripheral when transitioning from a disabled
  963. * bus state to any of the active bus states so that BIF
  964. * transactions can take place.
  965. */
  966. reg = QPNP_BSI_ENABLE;
  967. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_ENABLE, &reg, 1);
  968. if (rc) {
  969. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  970. __func__, rc);
  971. return rc;
  972. }
  973. }
  974. switch (state) {
  975. case BIF_BUS_STATE_MASTER_DISABLED:
  976. /* Disable the BSI peripheral. */
  977. reg = QPNP_BSI_DISABLE;
  978. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_ENABLE, &reg, 1);
  979. if (rc)
  980. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  981. __func__, rc);
  982. break;
  983. case BIF_BUS_STATE_POWER_DOWN:
  984. rc = qpnp_bsi_bus_transaction(bdev, BIF_TRANS_BC, BIF_CMD_PDWN);
  985. if (rc)
  986. dev_err(&chip->spmi_dev->dev, "%s: failed to enable power down mode, rc=%d\n",
  987. __func__, rc);
  988. break;
  989. case BIF_BUS_STATE_STANDBY:
  990. rc = qpnp_bsi_bus_transaction(bdev, BIF_TRANS_BC, BIF_CMD_STBY);
  991. if (rc)
  992. dev_err(&chip->spmi_dev->dev, "%s: failed to enable standby mode, rc=%d\n",
  993. __func__, rc);
  994. break;
  995. case BIF_BUS_STATE_ACTIVE:
  996. rc = qpnp_bsi_bus_set_active_mode(bdev, chip->state);
  997. if (rc)
  998. dev_err(&chip->spmi_dev->dev, "%s: failed to enable active mode, rc=%d\n",
  999. __func__, rc);
  1000. break;
  1001. case BIF_BUS_STATE_INTERRUPT:
  1002. /*
  1003. * qpnp_bsi_bus_set_interrupt_mode() internally sets
  1004. * chip->state = BIF_BUS_STATE_INTERRUPT immediately before
  1005. * issuing the EINT command.
  1006. */
  1007. rc = qpnp_bsi_bus_set_interrupt_mode(bdev);
  1008. if (rc) {
  1009. dev_err(&chip->spmi_dev->dev, "%s: failed to enable interrupt mode, rc=%d\n",
  1010. __func__, rc);
  1011. } else if (chip->state == BIF_BUS_STATE_ACTIVE) {
  1012. /*
  1013. * A slave interrupt was received immediately after
  1014. * issuing the EINT command. Therefore, stay in active
  1015. * communication mode.
  1016. */
  1017. state = BIF_BUS_STATE_ACTIVE;
  1018. }
  1019. break;
  1020. default:
  1021. rc = -EINVAL;
  1022. dev_err(&chip->spmi_dev->dev, "%s: invalid state=%d\n",
  1023. __func__, state);
  1024. }
  1025. if (!rc)
  1026. chip->state = state;
  1027. return rc;
  1028. }
  1029. /* Returns the smallest tau_bif that is greater than or equal to period_ns. */
  1030. static int qpnp_bsi_tau_bif_higher(int period_ns, int sample_mask)
  1031. {
  1032. const int *supported_period_ns =
  1033. (sample_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
  1034. qpnp_bsi_tau_period.period_4x_ns :
  1035. qpnp_bsi_tau_period.period_8x_ns);
  1036. int smallest_tau_bif = INT_MAX;
  1037. int i;
  1038. for (i = QPNP_BSI_NUM_CLOCK_PERIODS - 1; i >= 0; i--) {
  1039. if (period_ns <= supported_period_ns[i]) {
  1040. smallest_tau_bif = supported_period_ns[i];
  1041. break;
  1042. }
  1043. }
  1044. return smallest_tau_bif;
  1045. }
  1046. /* Returns the largest tau_bif that is less than or equal to period_ns. */
  1047. static int qpnp_bsi_tau_bif_lower(int period_ns, int sample_mask)
  1048. {
  1049. const int *supported_period_ns =
  1050. (sample_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
  1051. qpnp_bsi_tau_period.period_4x_ns :
  1052. qpnp_bsi_tau_period.period_8x_ns);
  1053. int largest_tau_bif = 0;
  1054. int i;
  1055. for (i = 0; i < QPNP_BSI_NUM_CLOCK_PERIODS; i++) {
  1056. if (period_ns >= supported_period_ns[i]) {
  1057. largest_tau_bif = supported_period_ns[i];
  1058. break;
  1059. }
  1060. }
  1061. return largest_tau_bif;
  1062. }
  1063. /*
  1064. * Moves period_ns into allowed range and then sets tau bif to the period that
  1065. * is greater than or equal to period_ns.
  1066. */
  1067. static int qpnp_bsi_set_tau_bif(struct qpnp_bsi_chip *chip, int period_ns)
  1068. {
  1069. const int *supported_period_ns =
  1070. (chip->tau_sampling_mask == QPNP_BSI_TAU_CONFIG_SAMPLE_4X ?
  1071. qpnp_bsi_tau_period.period_4x_ns :
  1072. qpnp_bsi_tau_period.period_8x_ns);
  1073. int idx = 0;
  1074. int i, rc;
  1075. u8 reg;
  1076. if (period_ns < chip->bdesc.bus_clock_min_ns)
  1077. period_ns = chip->bdesc.bus_clock_min_ns;
  1078. else if (period_ns > chip->bdesc.bus_clock_max_ns)
  1079. period_ns = chip->bdesc.bus_clock_max_ns;
  1080. for (i = QPNP_BSI_NUM_CLOCK_PERIODS - 1; i >= 0; i--) {
  1081. if (period_ns <= supported_period_ns[i]) {
  1082. idx = i;
  1083. break;
  1084. }
  1085. }
  1086. /* Set the tau BIF clock period and sampling rate. */
  1087. reg = chip->tau_sampling_mask | idx;
  1088. rc = qpnp_bsi_write(chip, QPNP_BSI_REG_TAU_CONFIG, &reg, 1);
  1089. if (rc) {
  1090. dev_err(&chip->spmi_dev->dev, "%s: qpnp_bsi_write() failed, rc=%d\n",
  1091. __func__, rc);
  1092. return rc;
  1093. }
  1094. chip->tau_index = idx;
  1095. return 0;
  1096. }
  1097. static int qpnp_bsi_get_bus_period(struct bif_ctrl_dev *bdev)
  1098. {
  1099. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  1100. return qpnp_bsi_get_tau_ns(chip);
  1101. }
  1102. static int qpnp_bsi_set_bus_period(struct bif_ctrl_dev *bdev, int period_ns)
  1103. {
  1104. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  1105. return qpnp_bsi_set_tau_bif(chip, period_ns);
  1106. }
  1107. static int qpnp_bsi_get_battery_rid(struct bif_ctrl_dev *bdev)
  1108. {
  1109. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  1110. struct qpnp_vadc_result adc_result;
  1111. int rid_ohm, vid_uV, rc;
  1112. s64 temp;
  1113. if (chip->batt_id_adc_channel >= ADC_MAX_NUM) {
  1114. dev_err(&chip->spmi_dev->dev, "%s: no ADC channel specified for Rid measurement\n",
  1115. __func__);
  1116. return -ENXIO;
  1117. }
  1118. rc = qpnp_vadc_read(chip->vadc_dev, chip->batt_id_adc_channel,
  1119. &adc_result);
  1120. if (!rc) {
  1121. vid_uV = adc_result.physical;
  1122. if (chip->vid_ref_uV - vid_uV <= 0) {
  1123. rid_ohm = INT_MAX;
  1124. } else {
  1125. temp = (s64)chip->r_pullup_ohm * (s64)vid_uV;
  1126. do_div(temp, chip->vid_ref_uV - vid_uV);
  1127. if (temp > INT_MAX)
  1128. rid_ohm = INT_MAX;
  1129. else
  1130. rid_ohm = temp;
  1131. }
  1132. } else {
  1133. dev_err(&chip->spmi_dev->dev, "%s: qpnp_vadc_read(%d) failed, rc=%d\n",
  1134. __func__, chip->batt_id_adc_channel, rc);
  1135. rid_ohm = rc;
  1136. }
  1137. return rid_ohm;
  1138. }
  1139. /*
  1140. * Returns 1 if a battery pack is present on the BIF bus, 0 if a battery pack
  1141. * is not present, or errno if detection fails.
  1142. *
  1143. * Battery detection is based upon the idle BCL voltage.
  1144. */
  1145. static int qpnp_bsi_get_battery_presence(struct bif_ctrl_dev *bdev)
  1146. {
  1147. struct qpnp_bsi_chip *chip = bdev_get_drvdata(bdev);
  1148. u8 reg = 0x00;
  1149. int rc;
  1150. rc = spmi_ext_register_readl(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  1151. chip->batt_id_stat_addr, &reg, 1);
  1152. if (rc) {
  1153. dev_err(&chip->spmi_dev->dev, "%s: spmi_ext_register_readl() failed, rc=%d\n",
  1154. __func__, rc);
  1155. return rc;
  1156. }
  1157. return !!(reg & QPNP_SMBB_BAT_IF_BATT_PRES_MASK);
  1158. }
  1159. static struct bif_ctrl_ops qpnp_bsi_ops = {
  1160. .bus_transaction = qpnp_bsi_bus_transaction,
  1161. .bus_transaction_query = qpnp_bsi_bus_transaction_query,
  1162. .bus_transaction_read = qpnp_bsi_bus_transaction_read,
  1163. .get_bus_state = qpnp_bsi_get_bus_state,
  1164. .set_bus_state = qpnp_bsi_set_bus_state,
  1165. .get_bus_period = qpnp_bsi_get_bus_period,
  1166. .set_bus_period = qpnp_bsi_set_bus_period,
  1167. .read_slave_registers = qpnp_bsi_read_slave_registers,
  1168. .write_slave_registers = qpnp_bsi_write_slave_registers,
  1169. .get_battery_rid = qpnp_bsi_get_battery_rid,
  1170. .get_battery_presence = qpnp_bsi_get_battery_presence,
  1171. };
  1172. /* Load all BSI properties from device tree. */
  1173. static int __devinit qpnp_bsi_parse_dt(struct qpnp_bsi_chip *chip,
  1174. struct spmi_device *spmi)
  1175. {
  1176. struct device *dev = &spmi->dev;
  1177. struct device_node *node = spmi->dev.of_node;
  1178. struct resource *res;
  1179. int rc, temp;
  1180. chip->batt_id_adc_channel = ADC_MAX_NUM;
  1181. rc = of_property_read_u32(node, "qcom,channel-num",
  1182. &chip->batt_id_adc_channel);
  1183. if (!rc && (chip->batt_id_adc_channel < 0
  1184. || chip->batt_id_adc_channel >= ADC_MAX_NUM)) {
  1185. dev_err(dev, "%s: invalid qcom,channel-num=%d specified\n",
  1186. __func__, chip->batt_id_adc_channel);
  1187. return -EINVAL;
  1188. }
  1189. chip->r_pullup_ohm = QPNP_BSI_DEFAULT_PULLUP_OHM;
  1190. rc = of_property_read_u32(node, "qcom,pullup-ohms",
  1191. &chip->r_pullup_ohm);
  1192. if (!rc && (chip->r_pullup_ohm < QPNP_BSI_MIN_PULLUP_OHM ||
  1193. chip->r_pullup_ohm > QPNP_BSI_MAX_PULLUP_OHM)) {
  1194. dev_err(dev, "%s: invalid qcom,pullup-ohms=%d property value\n",
  1195. __func__, chip->r_pullup_ohm);
  1196. return -EINVAL;
  1197. }
  1198. chip->vid_ref_uV = QPNP_BSI_DEFAULT_VID_REF_UV;
  1199. rc = of_property_read_u32(node, "qcom,vref-microvolts",
  1200. &chip->vid_ref_uV);
  1201. if (!rc && (chip->vid_ref_uV < QPNP_BSI_MIN_VID_REF_UV ||
  1202. chip->vid_ref_uV > QPNP_BSI_MAX_VID_REF_UV)) {
  1203. dev_err(dev, "%s: invalid qcom,vref-microvolts=%d property value\n",
  1204. __func__, chip->vid_ref_uV);
  1205. return -EINVAL;
  1206. }
  1207. res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM, "bsi-base");
  1208. if (!res) {
  1209. dev_err(dev, "%s: node is missing BSI base address\n",
  1210. __func__);
  1211. return -EINVAL;
  1212. }
  1213. chip->base_addr = res->start;
  1214. res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM,
  1215. "batt-id-status");
  1216. if (!res) {
  1217. dev_err(dev, "%s: node is missing BATT_ID status address\n",
  1218. __func__);
  1219. return -EINVAL;
  1220. }
  1221. chip->batt_id_stat_addr = res->start;
  1222. chip->bdesc.name = spmi_get_primary_dev_name(spmi);
  1223. if (!chip->bdesc.name) {
  1224. dev_err(dev, "%s: label binding undefined for node %s\n",
  1225. __func__, spmi->dev.of_node->full_name);
  1226. return -EINVAL;
  1227. }
  1228. /* Use maximum range by default. */
  1229. chip->bdesc.bus_clock_min_ns = QPNP_BSI_MIN_CLOCK_SPEED_NS;
  1230. chip->bdesc.bus_clock_max_ns = QPNP_BSI_MAX_CLOCK_SPEED_NS;
  1231. chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_4X;
  1232. rc = of_property_read_u32(node, "qcom,sample-rate", &temp);
  1233. if (rc == 0) {
  1234. if (temp == 4) {
  1235. chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_4X;
  1236. } else if (temp == 8) {
  1237. chip->tau_sampling_mask = QPNP_BSI_TAU_CONFIG_SAMPLE_8X;
  1238. } else {
  1239. dev_err(dev, "%s: invalid qcom,sample-rate=%d. Only values of 4 and 8 are supported.\n",
  1240. __func__, temp);
  1241. return -EINVAL;
  1242. }
  1243. }
  1244. rc = of_property_read_u32(node, "qcom,min-clock-period", &temp);
  1245. if (rc == 0)
  1246. chip->bdesc.bus_clock_min_ns = qpnp_bsi_tau_bif_higher(temp,
  1247. chip->tau_sampling_mask);
  1248. rc = of_property_read_u32(node, "qcom,max-clock-period", &temp);
  1249. if (rc == 0)
  1250. chip->bdesc.bus_clock_max_ns = qpnp_bsi_tau_bif_lower(temp,
  1251. chip->tau_sampling_mask);
  1252. if (chip->bdesc.bus_clock_min_ns > chip->bdesc.bus_clock_max_ns) {
  1253. dev_err(dev, "%s: invalid qcom,min/max-clock-period.\n",
  1254. __func__);
  1255. return -EINVAL;
  1256. }
  1257. chip->irq[QPNP_BSI_IRQ_ERR] = spmi_get_irq_byname(spmi, NULL, "err");
  1258. if (chip->irq[QPNP_BSI_IRQ_ERR] < 0) {
  1259. dev_err(dev, "%s: node is missing err irq\n", __func__);
  1260. return chip->irq[QPNP_BSI_IRQ_ERR];
  1261. }
  1262. chip->irq[QPNP_BSI_IRQ_RX] = spmi_get_irq_byname(spmi, NULL, "rx");
  1263. if (chip->irq[QPNP_BSI_IRQ_RX] < 0) {
  1264. dev_err(dev, "%s: node is missing rx irq\n", __func__);
  1265. return chip->irq[QPNP_BSI_IRQ_RX];
  1266. }
  1267. chip->irq[QPNP_BSI_IRQ_TX] = spmi_get_irq_byname(spmi, NULL, "tx");
  1268. if (chip->irq[QPNP_BSI_IRQ_TX] < 0) {
  1269. dev_err(dev, "%s: node is missing tx irq\n", __func__);
  1270. return chip->irq[QPNP_BSI_IRQ_TX];
  1271. }
  1272. chip->batt_present_irq = spmi_get_irq_byname(spmi, NULL,
  1273. "batt-present");
  1274. if (chip->batt_present_irq < 0) {
  1275. dev_err(dev, "%s: node is missing batt-present irq\n",
  1276. __func__);
  1277. return chip->batt_present_irq;
  1278. }
  1279. return rc;
  1280. }
  1281. /* Request all BSI and battery presence IRQs and set them as wakeable. */
  1282. static int __devinit qpnp_bsi_init_irqs(struct qpnp_bsi_chip *chip,
  1283. struct device *dev)
  1284. {
  1285. int rc;
  1286. rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_ERR],
  1287. qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-err", chip);
  1288. if (rc < 0) {
  1289. dev_err(dev, "%s: request for bsi-err irq %d failed, rc=%d\n",
  1290. __func__, chip->irq[QPNP_BSI_IRQ_ERR], rc);
  1291. return rc;
  1292. }
  1293. rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 1);
  1294. if (rc < 0) {
  1295. dev_err(dev, "%s: unable to set bsi-err irq %d as wakeable, rc=%d\n",
  1296. __func__, chip->irq[QPNP_BSI_IRQ_ERR], rc);
  1297. return rc;
  1298. }
  1299. rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_RX],
  1300. qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-rx", chip);
  1301. if (rc < 0) {
  1302. dev_err(dev, "%s: request for bsi-rx irq %d failed, rc=%d\n",
  1303. __func__, chip->irq[QPNP_BSI_IRQ_RX], rc);
  1304. goto set_unwakeable_irq_err;
  1305. }
  1306. rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 1);
  1307. if (rc < 0) {
  1308. dev_err(dev, "%s: unable to set bsi-rx irq %d as wakeable, rc=%d\n",
  1309. __func__, chip->irq[QPNP_BSI_IRQ_RX], rc);
  1310. goto set_unwakeable_irq_err;
  1311. }
  1312. rc = devm_request_irq(dev, chip->irq[QPNP_BSI_IRQ_TX],
  1313. qpnp_bsi_isr, IRQF_TRIGGER_HIGH, "bsi-tx", chip);
  1314. if (rc < 0) {
  1315. dev_err(dev, "%s: request for bsi-tx irq %d failed, rc=%d\n",
  1316. __func__, chip->irq[QPNP_BSI_IRQ_TX], rc);
  1317. goto set_unwakeable_irq_rx;
  1318. }
  1319. rc = irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 1);
  1320. if (rc < 0) {
  1321. dev_err(dev, "%s: unable to set bsi-tx irq %d as wakeable, rc=%d\n",
  1322. __func__, chip->irq[QPNP_BSI_IRQ_TX], rc);
  1323. goto set_unwakeable_irq_rx;
  1324. }
  1325. rc = devm_request_threaded_irq(dev, chip->batt_present_irq, NULL,
  1326. qpnp_bsi_batt_present_isr,
  1327. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_SHARED
  1328. | IRQF_ONESHOT,
  1329. "bsi-batt-present", chip);
  1330. if (rc < 0) {
  1331. dev_err(dev, "%s: request for bsi-batt-present irq %d failed, rc=%d\n",
  1332. __func__, chip->batt_present_irq, rc);
  1333. goto set_unwakeable_irq_tx;
  1334. }
  1335. rc = irq_set_irq_wake(chip->batt_present_irq, 1);
  1336. if (rc < 0) {
  1337. dev_err(dev, "%s: unable to set bsi-batt-present irq %d as wakeable, rc=%d\n",
  1338. __func__, chip->batt_present_irq, rc);
  1339. goto set_unwakeable_irq_tx;
  1340. }
  1341. return rc;
  1342. set_unwakeable_irq_tx:
  1343. irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 0);
  1344. set_unwakeable_irq_rx:
  1345. irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 0);
  1346. set_unwakeable_irq_err:
  1347. irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 0);
  1348. return rc;
  1349. }
  1350. static void qpnp_bsi_cleanup_irqs(struct qpnp_bsi_chip *chip)
  1351. {
  1352. irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_ERR], 0);
  1353. irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_RX], 0);
  1354. irq_set_irq_wake(chip->irq[QPNP_BSI_IRQ_TX], 0);
  1355. irq_set_irq_wake(chip->batt_present_irq, 0);
  1356. }
  1357. static int __devinit qpnp_bsi_probe(struct spmi_device *spmi)
  1358. {
  1359. struct device *dev = &spmi->dev;
  1360. struct qpnp_bsi_chip *chip;
  1361. int rc;
  1362. u8 type[2];
  1363. if (!spmi->dev.of_node) {
  1364. dev_err(dev, "%s: device node missing\n", __func__);
  1365. return -ENODEV;
  1366. }
  1367. chip = devm_kzalloc(dev, sizeof(struct qpnp_bsi_chip), GFP_KERNEL);
  1368. if (!chip) {
  1369. dev_err(dev, "%s: Can't allocate qpnp_bsi\n", __func__);
  1370. return -ENOMEM;
  1371. }
  1372. rc = qpnp_bsi_parse_dt(chip, spmi);
  1373. if (rc) {
  1374. dev_err(dev, "%s: device tree parsing failed, rc=%d\n",
  1375. __func__, rc);
  1376. return rc;
  1377. }
  1378. INIT_WORK(&chip->slave_irq_work, qpnp_bsi_slave_irq_work);
  1379. rc = qpnp_bsi_init_irqs(chip, dev);
  1380. if (rc) {
  1381. dev_err(dev, "%s: IRQ initialization failed, rc=%d\n",
  1382. __func__, rc);
  1383. return rc;
  1384. }
  1385. chip->spmi_dev = spmi;
  1386. chip->bdesc.ops = &qpnp_bsi_ops;
  1387. chip->state = BIF_BUS_STATE_MASTER_DISABLED;
  1388. chip->com_mode = QPNP_BSI_COM_MODE_IRQ;
  1389. rc = qpnp_bsi_read(chip, QPNP_BSI_REG_TYPE, type, 2);
  1390. if (rc) {
  1391. dev_err(dev, "%s: could not read type register, rc=%d\n",
  1392. __func__, rc);
  1393. goto cleanup_irqs;
  1394. }
  1395. if (type[0] != QPNP_BSI_TYPE || type[1] != QPNP_BSI_SUBTYPE) {
  1396. dev_err(dev, "%s: BSI peripheral is not present; type=0x%02X, subtype=0x%02X\n",
  1397. __func__, type[0], type[1]);
  1398. rc = -ENODEV;
  1399. goto cleanup_irqs;
  1400. }
  1401. /* Ensure that ADC channel is available if it was specified. */
  1402. if (chip->batt_id_adc_channel < ADC_MAX_NUM) {
  1403. chip->vadc_dev = qpnp_get_vadc(dev, "bsi");
  1404. if (IS_ERR(chip->vadc_dev)) {
  1405. rc = PTR_ERR(chip->vadc_dev);
  1406. if (rc != -EPROBE_DEFER)
  1407. pr_err("missing vadc property, rc=%d\n", rc);
  1408. /* Probe retry, do not print an error message */
  1409. goto cleanup_irqs;
  1410. }
  1411. }
  1412. rc = qpnp_bsi_set_tau_bif(chip, chip->bdesc.bus_clock_min_ns);
  1413. if (rc) {
  1414. dev_err(dev, "%s: qpnp_bsi_set_tau_bif() failed, rc=%d\n",
  1415. __func__, rc);
  1416. goto cleanup_irqs;
  1417. }
  1418. chip->bdev = bif_ctrl_register(&chip->bdesc, dev, chip,
  1419. spmi->dev.of_node);
  1420. if (IS_ERR(chip->bdev)) {
  1421. rc = PTR_ERR(chip->bdev);
  1422. dev_err(dev, "%s: bif_ctrl_register failed, rc=%d\n",
  1423. __func__, rc);
  1424. goto cleanup_irqs;
  1425. }
  1426. dev_set_drvdata(dev, chip);
  1427. return rc;
  1428. cleanup_irqs:
  1429. qpnp_bsi_cleanup_irqs(chip);
  1430. return rc;
  1431. }
  1432. static int __devexit qpnp_bsi_remove(struct spmi_device *spmi)
  1433. {
  1434. struct qpnp_bsi_chip *chip = dev_get_drvdata(&spmi->dev);
  1435. dev_set_drvdata(&spmi->dev, NULL);
  1436. if (chip) {
  1437. bif_ctrl_unregister(chip->bdev);
  1438. qpnp_bsi_cleanup_irqs(chip);
  1439. }
  1440. return 0;
  1441. }
  1442. static struct of_device_id spmi_match_table[] = {
  1443. { .compatible = QPNP_BSI_DRIVER_NAME, },
  1444. {}
  1445. };
  1446. static const struct spmi_device_id qpnp_bsi_id[] = {
  1447. { QPNP_BSI_DRIVER_NAME, 0 },
  1448. { }
  1449. };
  1450. MODULE_DEVICE_TABLE(spmi, qpnp_bsi_id);
  1451. static struct spmi_driver qpnp_bsi_driver = {
  1452. .driver = {
  1453. .name = QPNP_BSI_DRIVER_NAME,
  1454. .of_match_table = spmi_match_table,
  1455. .owner = THIS_MODULE,
  1456. },
  1457. .probe = qpnp_bsi_probe,
  1458. .remove = __devexit_p(qpnp_bsi_remove),
  1459. .id_table = qpnp_bsi_id,
  1460. };
  1461. static int __init qpnp_bsi_init(void)
  1462. {
  1463. return spmi_driver_register(&qpnp_bsi_driver);
  1464. }
  1465. static void __exit qpnp_bsi_exit(void)
  1466. {
  1467. spmi_driver_unregister(&qpnp_bsi_driver);
  1468. }
  1469. MODULE_DESCRIPTION("QPNP PMIC BSI driver");
  1470. MODULE_LICENSE("GPL v2");
  1471. arch_initcall(qpnp_bsi_init);
  1472. module_exit(qpnp_bsi_exit);