qpnp-regulator.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/string.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/bitops.h>
  22. #include <linux/slab.h>
  23. #include <linux/spmi.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/ktime.h>
  28. #include <linux/regulator/driver.h>
  29. #include <linux/regulator/of_regulator.h>
  30. #include <linux/regulator/qpnp-regulator.h>
  31. /* Debug Flag Definitions */
  32. enum {
  33. QPNP_VREG_DEBUG_REQUEST = BIT(0), /* Show requests */
  34. QPNP_VREG_DEBUG_DUPLICATE = BIT(1), /* Show duplicate requests */
  35. QPNP_VREG_DEBUG_INIT = BIT(2), /* Show state after probe */
  36. QPNP_VREG_DEBUG_WRITES = BIT(3), /* Show SPMI writes */
  37. QPNP_VREG_DEBUG_READS = BIT(4), /* Show SPMI reads */
  38. QPNP_VREG_DEBUG_OCP = BIT(5), /* Show VS OCP IRQ events */
  39. };
  40. static int qpnp_vreg_debug_mask;
  41. module_param_named(
  42. debug_mask, qpnp_vreg_debug_mask, int, S_IRUSR | S_IWUSR
  43. );
  44. #define vreg_err(vreg, fmt, ...) \
  45. pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
  46. /* These types correspond to unique register layouts. */
  47. enum qpnp_regulator_logical_type {
  48. QPNP_REGULATOR_LOGICAL_TYPE_SMPS,
  49. QPNP_REGULATOR_LOGICAL_TYPE_LDO,
  50. QPNP_REGULATOR_LOGICAL_TYPE_VS,
  51. QPNP_REGULATOR_LOGICAL_TYPE_BOOST,
  52. QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS,
  53. };
  54. enum qpnp_regulator_type {
  55. QPNP_REGULATOR_TYPE_BUCK = 0x03,
  56. QPNP_REGULATOR_TYPE_LDO = 0x04,
  57. QPNP_REGULATOR_TYPE_VS = 0x05,
  58. QPNP_REGULATOR_TYPE_BOOST = 0x1B,
  59. QPNP_REGULATOR_TYPE_FTS = 0x1C,
  60. };
  61. enum qpnp_regulator_subtype {
  62. QPNP_REGULATOR_SUBTYPE_GP_CTL = 0x08,
  63. QPNP_REGULATOR_SUBTYPE_RF_CTL = 0x09,
  64. QPNP_REGULATOR_SUBTYPE_N50 = 0x01,
  65. QPNP_REGULATOR_SUBTYPE_N150 = 0x02,
  66. QPNP_REGULATOR_SUBTYPE_N300 = 0x03,
  67. QPNP_REGULATOR_SUBTYPE_N600 = 0x04,
  68. QPNP_REGULATOR_SUBTYPE_N1200 = 0x05,
  69. QPNP_REGULATOR_SUBTYPE_N600_ST = 0x06,
  70. QPNP_REGULATOR_SUBTYPE_N1200_ST = 0x07,
  71. QPNP_REGULATOR_SUBTYPE_P50 = 0x08,
  72. QPNP_REGULATOR_SUBTYPE_P150 = 0x09,
  73. QPNP_REGULATOR_SUBTYPE_P300 = 0x0A,
  74. QPNP_REGULATOR_SUBTYPE_P600 = 0x0B,
  75. QPNP_REGULATOR_SUBTYPE_P1200 = 0x0C,
  76. QPNP_REGULATOR_SUBTYPE_LV_P50 = 0x28,
  77. QPNP_REGULATOR_SUBTYPE_LV_P150 = 0x29,
  78. QPNP_REGULATOR_SUBTYPE_LV_P300 = 0x2A,
  79. QPNP_REGULATOR_SUBTYPE_LV_P600 = 0x2B,
  80. QPNP_REGULATOR_SUBTYPE_LV_P1200 = 0x2C,
  81. QPNP_REGULATOR_SUBTYPE_LV100 = 0x01,
  82. QPNP_REGULATOR_SUBTYPE_LV300 = 0x02,
  83. QPNP_REGULATOR_SUBTYPE_MV300 = 0x08,
  84. QPNP_REGULATOR_SUBTYPE_MV500 = 0x09,
  85. QPNP_REGULATOR_SUBTYPE_HDMI = 0x10,
  86. QPNP_REGULATOR_SUBTYPE_OTG = 0x11,
  87. QPNP_REGULATOR_SUBTYPE_5V_BOOST = 0x01,
  88. QPNP_REGULATOR_SUBTYPE_FTS_CTL = 0x08,
  89. };
  90. enum qpnp_common_regulator_registers {
  91. QPNP_COMMON_REG_DIG_MAJOR_REV = 0x01,
  92. QPNP_COMMON_REG_TYPE = 0x04,
  93. QPNP_COMMON_REG_SUBTYPE = 0x05,
  94. QPNP_COMMON_REG_VOLTAGE_RANGE = 0x40,
  95. QPNP_COMMON_REG_VOLTAGE_SET = 0x41,
  96. QPNP_COMMON_REG_MODE = 0x45,
  97. QPNP_COMMON_REG_ENABLE = 0x46,
  98. QPNP_COMMON_REG_PULL_DOWN = 0x48,
  99. };
  100. enum qpnp_ldo_registers {
  101. QPNP_LDO_REG_SOFT_START = 0x4C,
  102. };
  103. enum qpnp_vs_registers {
  104. QPNP_VS_REG_OCP = 0x4A,
  105. QPNP_VS_REG_SOFT_START = 0x4C,
  106. };
  107. enum qpnp_boost_registers {
  108. QPNP_BOOST_REG_CURRENT_LIMIT = 0x4A,
  109. };
  110. /* Used for indexing into ctrl_reg. These are offets from 0x40 */
  111. enum qpnp_common_control_register_index {
  112. QPNP_COMMON_IDX_VOLTAGE_RANGE = 0,
  113. QPNP_COMMON_IDX_VOLTAGE_SET = 1,
  114. QPNP_COMMON_IDX_MODE = 5,
  115. QPNP_COMMON_IDX_ENABLE = 6,
  116. };
  117. /* Common regulator control register layout */
  118. #define QPNP_COMMON_ENABLE_MASK 0x80
  119. #define QPNP_COMMON_ENABLE 0x80
  120. #define QPNP_COMMON_DISABLE 0x00
  121. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
  122. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
  123. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
  124. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
  125. #define QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0F
  126. /* Common regulator mode register layout */
  127. #define QPNP_COMMON_MODE_HPM_MASK 0x80
  128. #define QPNP_COMMON_MODE_AUTO_MASK 0x40
  129. #define QPNP_COMMON_MODE_BYPASS_MASK 0x20
  130. #define QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
  131. #define QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
  132. #define QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
  133. #define QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
  134. #define QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
  135. #define QPNP_COMMON_MODE_FOLLOW_ALL_MASK 0x1F
  136. /* Common regulator pull down control register layout */
  137. #define QPNP_COMMON_PULL_DOWN_ENABLE_MASK 0x80
  138. /* LDO regulator current limit control register layout */
  139. #define QPNP_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
  140. /* LDO regulator soft start control register layout */
  141. #define QPNP_LDO_SOFT_START_ENABLE_MASK 0x80
  142. /* VS regulator over current protection control register layout */
  143. #define QPNP_VS_OCP_OVERRIDE 0x01
  144. #define QPNP_VS_OCP_NO_OVERRIDE 0x00
  145. /* VS regulator soft start control register layout */
  146. #define QPNP_VS_SOFT_START_ENABLE_MASK 0x80
  147. #define QPNP_VS_SOFT_START_SEL_MASK 0x03
  148. /* Boost regulator current limit control register layout */
  149. #define QPNP_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
  150. #define QPNP_BOOST_CURRENT_LIMIT_MASK 0x07
  151. #define QPNP_VS_OCP_DEFAULT_MAX_RETRIES 10
  152. #define QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS 30
  153. #define QPNP_VS_OCP_FALL_DELAY_US 90
  154. #define QPNP_VS_OCP_FAULT_DELAY_US 20000
  155. /*
  156. * This voltage in uV is returned by get_voltage functions when there is no way
  157. * to determine the current voltage level. It is needed because the regulator
  158. * framework treats a 0 uV voltage as an error.
  159. */
  160. #define VOLTAGE_UNKNOWN 1
  161. struct qpnp_voltage_range {
  162. int min_uV;
  163. int max_uV;
  164. int step_uV;
  165. int set_point_min_uV;
  166. unsigned n_voltages;
  167. u8 range_sel;
  168. };
  169. struct qpnp_voltage_set_points {
  170. struct qpnp_voltage_range *range;
  171. int count;
  172. unsigned n_voltages;
  173. };
  174. struct qpnp_regulator_mapping {
  175. enum qpnp_regulator_type type;
  176. enum qpnp_regulator_subtype subtype;
  177. enum qpnp_regulator_logical_type logical_type;
  178. u32 revision_min;
  179. u32 revision_max;
  180. struct regulator_ops *ops;
  181. struct qpnp_voltage_set_points *set_points;
  182. int hpm_min_load;
  183. };
  184. struct qpnp_regulator {
  185. struct regulator_desc rdesc;
  186. struct delayed_work ocp_work;
  187. struct spmi_device *spmi_dev;
  188. struct regulator_dev *rdev;
  189. struct qpnp_voltage_set_points *set_points;
  190. enum qpnp_regulator_logical_type logical_type;
  191. int enable_time;
  192. int ocp_enable;
  193. int ocp_irq;
  194. int ocp_count;
  195. int ocp_max_retries;
  196. int ocp_retry_delay_ms;
  197. int system_load;
  198. int hpm_min_load;
  199. u32 write_count;
  200. u32 prev_write_count;
  201. ktime_t vs_enable_time;
  202. u16 base_addr;
  203. /* ctrl_reg provides a shadow copy of register values 0x40 to 0x47. */
  204. u8 ctrl_reg[8];
  205. };
  206. #define QPNP_VREG_MAP(_type, _subtype, _dig_major_min, _dig_major_max, \
  207. _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
  208. { \
  209. .type = QPNP_REGULATOR_TYPE_##_type, \
  210. .subtype = QPNP_REGULATOR_SUBTYPE_##_subtype, \
  211. .revision_min = _dig_major_min, \
  212. .revision_max = _dig_major_max, \
  213. .logical_type = QPNP_REGULATOR_LOGICAL_TYPE_##_logical_type, \
  214. .ops = &qpnp_##_ops_val##_ops, \
  215. .set_points = &_set_points_val##_set_points, \
  216. .hpm_min_load = _hpm_min_load, \
  217. }
  218. #define VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, _max_uV, \
  219. _step_uV) \
  220. { \
  221. .min_uV = _min_uV, \
  222. .set_point_min_uV = _set_point_min_uV, \
  223. .max_uV = _max_uV, \
  224. .step_uV = _step_uV, \
  225. .range_sel = _range_sel, \
  226. }
  227. #define SET_POINTS(_ranges) \
  228. { \
  229. .range = _ranges, \
  230. .count = ARRAY_SIZE(_ranges), \
  231. };
  232. /*
  233. * These tables contain the physically available PMIC regulator voltage setpoint
  234. * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
  235. * to ensure that the setpoints available to software are monotonically
  236. * increasing and unique. The set_voltage callback functions expect these
  237. * properties to hold.
  238. */
  239. static struct qpnp_voltage_range pldo_ranges[] = {
  240. VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
  241. VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 25000),
  242. VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 50000),
  243. };
  244. static struct qpnp_voltage_range nldo1_ranges[] = {
  245. VOLTAGE_RANGE(2, 750000, 750000, 1537500, 12500),
  246. };
  247. static struct qpnp_voltage_range nldo2_ranges[] = {
  248. VOLTAGE_RANGE(1, 375000, 375000, 768750, 6250),
  249. VOLTAGE_RANGE(2, 750000, 775000, 1537500, 12500),
  250. };
  251. static struct qpnp_voltage_range nldo3_ranges[] = {
  252. VOLTAGE_RANGE(0, 375000, 375000, 1537500, 12500),
  253. };
  254. static struct qpnp_voltage_range smps_ranges[] = {
  255. VOLTAGE_RANGE(0, 375000, 375000, 1562500, 12500),
  256. VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 25000),
  257. };
  258. static struct qpnp_voltage_range ftsmps_ranges[] = {
  259. VOLTAGE_RANGE(0, 0, 350000, 1275000, 5000),
  260. VOLTAGE_RANGE(1, 0, 1280000, 2040000, 10000),
  261. };
  262. static struct qpnp_voltage_range boost_ranges[] = {
  263. VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 50000),
  264. };
  265. static struct qpnp_voltage_set_points pldo_set_points = SET_POINTS(pldo_ranges);
  266. static struct qpnp_voltage_set_points nldo1_set_points
  267. = SET_POINTS(nldo1_ranges);
  268. static struct qpnp_voltage_set_points nldo2_set_points
  269. = SET_POINTS(nldo2_ranges);
  270. static struct qpnp_voltage_set_points nldo3_set_points
  271. = SET_POINTS(nldo3_ranges);
  272. static struct qpnp_voltage_set_points smps_set_points = SET_POINTS(smps_ranges);
  273. static struct qpnp_voltage_set_points ftsmps_set_points
  274. = SET_POINTS(ftsmps_ranges);
  275. static struct qpnp_voltage_set_points boost_set_points
  276. = SET_POINTS(boost_ranges);
  277. static struct qpnp_voltage_set_points none_set_points;
  278. static struct qpnp_voltage_set_points *all_set_points[] = {
  279. &pldo_set_points,
  280. &nldo1_set_points,
  281. &nldo2_set_points,
  282. &nldo3_set_points,
  283. &smps_set_points,
  284. &ftsmps_set_points,
  285. &boost_set_points,
  286. };
  287. /* Determines which label to add to a debug print statement. */
  288. enum qpnp_regulator_action {
  289. QPNP_REGULATOR_ACTION_INIT,
  290. QPNP_REGULATOR_ACTION_ENABLE,
  291. QPNP_REGULATOR_ACTION_DISABLE,
  292. QPNP_REGULATOR_ACTION_VOLTAGE,
  293. QPNP_REGULATOR_ACTION_MODE,
  294. };
  295. static void qpnp_vreg_show_state(struct regulator_dev *rdev,
  296. enum qpnp_regulator_action action);
  297. #define DEBUG_PRINT_BUFFER_SIZE 64
  298. static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len)
  299. {
  300. int pos = 0;
  301. int i;
  302. for (i = 0; i < buf_len; i++) {
  303. pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]);
  304. if (i < buf_len - 1)
  305. pos += scnprintf(str + pos, str_len - pos, ", ");
  306. }
  307. }
  308. static inline int qpnp_vreg_read(struct qpnp_regulator *vreg, u16 addr, u8 *buf,
  309. int len)
  310. {
  311. char str[DEBUG_PRINT_BUFFER_SIZE];
  312. int rc = 0;
  313. rc = spmi_ext_register_readl(vreg->spmi_dev->ctrl, vreg->spmi_dev->sid,
  314. vreg->base_addr + addr, buf, len);
  315. if (!rc && (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_READS)) {
  316. str[0] = '\0';
  317. fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
  318. pr_info(" %-11s: read(0x%04X), sid=%d, len=%d; %s\n",
  319. vreg->rdesc.name, vreg->base_addr + addr,
  320. vreg->spmi_dev->sid, len, str);
  321. }
  322. return rc;
  323. }
  324. static inline int qpnp_vreg_write(struct qpnp_regulator *vreg, u16 addr,
  325. u8 *buf, int len)
  326. {
  327. char str[DEBUG_PRINT_BUFFER_SIZE];
  328. int rc = 0;
  329. if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_WRITES) {
  330. str[0] = '\0';
  331. fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
  332. pr_info("%-11s: write(0x%04X), sid=%d, len=%d; %s\n",
  333. vreg->rdesc.name, vreg->base_addr + addr,
  334. vreg->spmi_dev->sid, len, str);
  335. }
  336. rc = spmi_ext_register_writel(vreg->spmi_dev->ctrl,
  337. vreg->spmi_dev->sid, vreg->base_addr + addr, buf, len);
  338. if (!rc)
  339. vreg->write_count += len;
  340. return rc;
  341. }
  342. /*
  343. * qpnp_vreg_write_optimized - write the minimum sized contiguous subset of buf
  344. * @vreg: qpnp_regulator pointer for this regulator
  345. * @addr: local SPMI address offset from this peripheral's base address
  346. * @buf: new data to write into the SPMI registers
  347. * @buf_save: old data in the registers
  348. * @len: number of bytes to write
  349. *
  350. * This function checks for unchanged register values between buf and buf_save
  351. * starting at both ends of buf. Only the contiguous subset in the middle of
  352. * buf starting and ending with new values is sent.
  353. *
  354. * Consider the following example:
  355. * buf offset: 0 1 2 3 4 5 6 7
  356. * reg state: U U C C U C U U
  357. * (U = unchanged, C = changed)
  358. * In this example registers 2 through 5 will be written with a single
  359. * transaction.
  360. */
  361. static inline int qpnp_vreg_write_optimized(struct qpnp_regulator *vreg,
  362. u16 addr, u8 *buf, u8 *buf_save, int len)
  363. {
  364. int i, rc, start, end;
  365. for (i = 0; i < len; i++)
  366. if (buf[i] != buf_save[i])
  367. break;
  368. start = i;
  369. for (i = len - 1; i >= 0; i--)
  370. if (buf[i] != buf_save[i])
  371. break;
  372. end = i;
  373. if (start > end) {
  374. /* No modified register values present. */
  375. return 0;
  376. }
  377. rc = qpnp_vreg_write(vreg, addr + start, &buf[start], end - start + 1);
  378. if (!rc)
  379. for (i = start; i <= end; i++)
  380. buf_save[i] = buf[i];
  381. return rc;
  382. }
  383. /*
  384. * Perform a masked write to a PMIC register only if the new value differs
  385. * from the last value written to the register. This removes redundant
  386. * register writing.
  387. */
  388. static int qpnp_vreg_masked_write(struct qpnp_regulator *vreg, u16 addr, u8 val,
  389. u8 mask, u8 *reg_save)
  390. {
  391. int rc = 0;
  392. u8 reg;
  393. reg = (*reg_save & ~mask) | (val & mask);
  394. if (reg != *reg_save) {
  395. rc = qpnp_vreg_write(vreg, addr, &reg, 1);
  396. if (rc) {
  397. vreg_err(vreg, "write failed; addr=0x%03X, rc=%d\n",
  398. addr, rc);
  399. } else {
  400. *reg_save = reg;
  401. }
  402. }
  403. return rc;
  404. }
  405. /*
  406. * Perform a masked read-modify-write to a PMIC register only if the new value
  407. * differs from the value currently in the register. This removes redundant
  408. * register writing.
  409. */
  410. static int qpnp_vreg_masked_read_write(struct qpnp_regulator *vreg, u16 addr,
  411. u8 val, u8 mask)
  412. {
  413. int rc;
  414. u8 reg;
  415. rc = qpnp_vreg_read(vreg, addr, &reg, 1);
  416. if (rc) {
  417. vreg_err(vreg, "read failed; addr=0x%03X, rc=%d\n", addr, rc);
  418. return rc;
  419. }
  420. return qpnp_vreg_masked_write(vreg, addr, val, mask, &reg);
  421. }
  422. static int qpnp_regulator_common_is_enabled(struct regulator_dev *rdev)
  423. {
  424. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  425. return (vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]
  426. & QPNP_COMMON_ENABLE_MASK)
  427. == QPNP_COMMON_ENABLE;
  428. }
  429. static int qpnp_regulator_common_enable(struct regulator_dev *rdev)
  430. {
  431. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  432. int rc;
  433. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  434. QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
  435. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  436. if (rc)
  437. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  438. else
  439. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_ENABLE);
  440. return rc;
  441. }
  442. static int qpnp_regulator_vs_enable(struct regulator_dev *rdev)
  443. {
  444. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  445. if (vreg->ocp_irq) {
  446. vreg->ocp_count = 0;
  447. vreg->vs_enable_time = ktime_get();
  448. }
  449. return qpnp_regulator_common_enable(rdev);
  450. }
  451. static int qpnp_regulator_common_disable(struct regulator_dev *rdev)
  452. {
  453. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  454. int rc;
  455. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  456. QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
  457. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  458. if (rc)
  459. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  460. else
  461. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_DISABLE);
  462. return rc;
  463. }
  464. static int qpnp_regulator_select_voltage(struct qpnp_regulator *vreg,
  465. int min_uV, int max_uV, int *range_sel, int *voltage_sel,
  466. unsigned *selector)
  467. {
  468. struct qpnp_voltage_range *range;
  469. int uV = min_uV;
  470. int lim_min_uV, lim_max_uV, i, range_id;
  471. /* Check if request voltage is outside of physically settable range. */
  472. lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
  473. lim_max_uV =
  474. vreg->set_points->range[vreg->set_points->count - 1].max_uV;
  475. if (uV < lim_min_uV && max_uV >= lim_min_uV)
  476. uV = lim_min_uV;
  477. if (uV < lim_min_uV || uV > lim_max_uV) {
  478. vreg_err(vreg,
  479. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  480. min_uV, max_uV, lim_min_uV, lim_max_uV);
  481. return -EINVAL;
  482. }
  483. /* Find the range which uV is inside of. */
  484. for (i = vreg->set_points->count - 1; i > 0; i--)
  485. if (uV > vreg->set_points->range[i - 1].max_uV)
  486. break;
  487. range_id = i;
  488. range = &vreg->set_points->range[range_id];
  489. *range_sel = range->range_sel;
  490. /*
  491. * Force uV to be an allowed set point by applying a ceiling function to
  492. * the uV value.
  493. */
  494. *voltage_sel = (uV - range->min_uV + range->step_uV - 1)
  495. / range->step_uV;
  496. uV = *voltage_sel * range->step_uV + range->min_uV;
  497. if (uV > max_uV) {
  498. vreg_err(vreg,
  499. "request v=[%d, %d] cannot be met by any set point; "
  500. "next set point: %d\n",
  501. min_uV, max_uV, uV);
  502. return -EINVAL;
  503. }
  504. *selector = 0;
  505. for (i = 0; i < range_id; i++)
  506. *selector += vreg->set_points->range[i].n_voltages;
  507. *selector += (uV - range->set_point_min_uV) / range->step_uV;
  508. return 0;
  509. }
  510. static int qpnp_regulator_common_set_voltage(struct regulator_dev *rdev,
  511. int min_uV, int max_uV, unsigned *selector)
  512. {
  513. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  514. int rc, range_sel, voltage_sel;
  515. u8 buf[2];
  516. rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
  517. &voltage_sel, selector);
  518. if (rc) {
  519. vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
  520. return rc;
  521. }
  522. buf[0] = range_sel;
  523. buf[1] = voltage_sel;
  524. if ((vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] != range_sel)
  525. && (vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] == voltage_sel)) {
  526. /* Handle latched range change. */
  527. rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
  528. buf, 2);
  529. if (!rc) {
  530. vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = buf[0];
  531. vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] = buf[1];
  532. }
  533. } else {
  534. /* Either write can be optimized away safely. */
  535. rc = qpnp_vreg_write_optimized(vreg,
  536. QPNP_COMMON_REG_VOLTAGE_RANGE, buf,
  537. &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE], 2);
  538. }
  539. if (rc)
  540. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  541. else
  542. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
  543. return rc;
  544. }
  545. static int qpnp_regulator_common_get_voltage(struct regulator_dev *rdev)
  546. {
  547. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  548. struct qpnp_voltage_range *range = NULL;
  549. int range_sel, voltage_sel, i;
  550. range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
  551. voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
  552. for (i = 0; i < vreg->set_points->count; i++) {
  553. if (vreg->set_points->range[i].range_sel == range_sel) {
  554. range = &vreg->set_points->range[i];
  555. break;
  556. }
  557. }
  558. if (!range) {
  559. vreg_err(vreg, "voltage unknown, range %d is invalid\n",
  560. range_sel);
  561. return VOLTAGE_UNKNOWN;
  562. }
  563. return range->step_uV * voltage_sel + range->min_uV;
  564. }
  565. static int qpnp_regulator_boost_set_voltage(struct regulator_dev *rdev,
  566. int min_uV, int max_uV, unsigned *selector)
  567. {
  568. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  569. int rc, range_sel, voltage_sel;
  570. rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
  571. &voltage_sel, selector);
  572. if (rc) {
  573. vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
  574. return rc;
  575. }
  576. /*
  577. * Boost type regulators do not have range select register so only
  578. * voltage set register needs to be written.
  579. */
  580. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET,
  581. voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]);
  582. if (rc)
  583. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  584. else
  585. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
  586. return rc;
  587. }
  588. static int qpnp_regulator_boost_get_voltage(struct regulator_dev *rdev)
  589. {
  590. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  591. int voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
  592. return boost_ranges[0].step_uV * voltage_sel + boost_ranges[0].min_uV;
  593. }
  594. static int qpnp_regulator_common_list_voltage(struct regulator_dev *rdev,
  595. unsigned selector)
  596. {
  597. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  598. int uV = 0;
  599. int i;
  600. if (selector >= vreg->set_points->n_voltages)
  601. return 0;
  602. for (i = 0; i < vreg->set_points->count; i++) {
  603. if (selector < vreg->set_points->range[i].n_voltages) {
  604. uV = selector * vreg->set_points->range[i].step_uV
  605. + vreg->set_points->range[i].set_point_min_uV;
  606. break;
  607. } else {
  608. selector -= vreg->set_points->range[i].n_voltages;
  609. }
  610. }
  611. return uV;
  612. }
  613. static unsigned int qpnp_regulator_common_get_mode(struct regulator_dev *rdev)
  614. {
  615. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  616. return (vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]
  617. & QPNP_COMMON_MODE_HPM_MASK)
  618. ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
  619. }
  620. static int qpnp_regulator_common_set_mode(struct regulator_dev *rdev,
  621. unsigned int mode)
  622. {
  623. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  624. int rc = 0;
  625. u8 val;
  626. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
  627. vreg_err(vreg, "invalid mode: %u\n", mode);
  628. return -EINVAL;
  629. }
  630. val = (mode == REGULATOR_MODE_NORMAL ? QPNP_COMMON_MODE_HPM_MASK : 0);
  631. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_MODE, val,
  632. QPNP_COMMON_MODE_HPM_MASK,
  633. &vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]);
  634. if (rc)
  635. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  636. else
  637. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE);
  638. return rc;
  639. }
  640. static unsigned int qpnp_regulator_common_get_optimum_mode(
  641. struct regulator_dev *rdev, int input_uV, int output_uV,
  642. int load_uA)
  643. {
  644. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  645. unsigned int mode;
  646. if (load_uA + vreg->system_load >= vreg->hpm_min_load)
  647. mode = REGULATOR_MODE_NORMAL;
  648. else
  649. mode = REGULATOR_MODE_IDLE;
  650. return mode;
  651. }
  652. static int qpnp_regulator_common_enable_time(struct regulator_dev *rdev)
  653. {
  654. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  655. return vreg->enable_time;
  656. }
  657. static int qpnp_regulator_vs_clear_ocp(struct qpnp_regulator *vreg)
  658. {
  659. int rc;
  660. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  661. QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
  662. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  663. if (rc)
  664. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  665. vreg->vs_enable_time = ktime_get();
  666. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  667. QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
  668. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  669. if (rc)
  670. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  671. if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
  672. pr_info("%s: switch state toggled after OCP event\n",
  673. vreg->rdesc.name);
  674. }
  675. return rc;
  676. }
  677. static void qpnp_regulator_vs_ocp_work(struct work_struct *work)
  678. {
  679. struct delayed_work *dwork
  680. = container_of(work, struct delayed_work, work);
  681. struct qpnp_regulator *vreg
  682. = container_of(dwork, struct qpnp_regulator, ocp_work);
  683. qpnp_regulator_vs_clear_ocp(vreg);
  684. return;
  685. }
  686. static irqreturn_t qpnp_regulator_vs_ocp_isr(int irq, void *data)
  687. {
  688. struct qpnp_regulator *vreg = data;
  689. ktime_t ocp_irq_time;
  690. s64 ocp_trigger_delay_us;
  691. ocp_irq_time = ktime_get();
  692. ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
  693. vreg->vs_enable_time);
  694. /*
  695. * Reset the OCP count if there is a large delay between switch enable
  696. * and when OCP triggers. This is indicative of a hotplug event as
  697. * opposed to a fault.
  698. */
  699. if (ocp_trigger_delay_us > QPNP_VS_OCP_FAULT_DELAY_US)
  700. vreg->ocp_count = 0;
  701. /* Wait for switch output to settle back to 0 V after OCP triggered. */
  702. udelay(QPNP_VS_OCP_FALL_DELAY_US);
  703. vreg->ocp_count++;
  704. if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
  705. pr_info("%s: VS OCP triggered, count = %d, delay = %lld us\n",
  706. vreg->rdesc.name, vreg->ocp_count,
  707. ocp_trigger_delay_us);
  708. }
  709. if (vreg->ocp_count == 1) {
  710. /* Immediately clear the over current condition. */
  711. qpnp_regulator_vs_clear_ocp(vreg);
  712. } else if (vreg->ocp_count <= vreg->ocp_max_retries) {
  713. /* Schedule the over current clear task to run later. */
  714. schedule_delayed_work(&vreg->ocp_work,
  715. msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
  716. } else {
  717. vreg_err(vreg, "OCP triggered %d times; no further retries\n",
  718. vreg->ocp_count);
  719. }
  720. return IRQ_HANDLED;
  721. }
  722. static const char const *qpnp_print_actions[] = {
  723. [QPNP_REGULATOR_ACTION_INIT] = "initial ",
  724. [QPNP_REGULATOR_ACTION_ENABLE] = "enable ",
  725. [QPNP_REGULATOR_ACTION_DISABLE] = "disable ",
  726. [QPNP_REGULATOR_ACTION_VOLTAGE] = "set voltage",
  727. [QPNP_REGULATOR_ACTION_MODE] = "set mode ",
  728. };
  729. static void qpnp_vreg_show_state(struct regulator_dev *rdev,
  730. enum qpnp_regulator_action action)
  731. {
  732. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  733. const char *action_label = qpnp_print_actions[action];
  734. unsigned int mode = 0;
  735. int uV = 0;
  736. const char *mode_label = "";
  737. enum qpnp_regulator_logical_type type;
  738. const char *enable_label;
  739. char pc_enable_label[5] = {'\0'};
  740. char pc_mode_label[8] = {'\0'};
  741. bool show_req, show_dupe, show_init, has_changed;
  742. u8 en_reg, mode_reg;
  743. /* Do not print unless appropriate flags are set. */
  744. show_req = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_REQUEST;
  745. show_dupe = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_DUPLICATE;
  746. show_init = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT;
  747. has_changed = vreg->write_count != vreg->prev_write_count;
  748. if (!((show_init && action == QPNP_REGULATOR_ACTION_INIT)
  749. || (show_req && (has_changed || show_dupe)))) {
  750. return;
  751. }
  752. vreg->prev_write_count = vreg->write_count;
  753. type = vreg->logical_type;
  754. enable_label = qpnp_regulator_common_is_enabled(rdev) ? "on " : "off";
  755. if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  756. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  757. || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
  758. uV = qpnp_regulator_common_get_voltage(rdev);
  759. if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST)
  760. uV = qpnp_regulator_boost_get_voltage(rdev);
  761. if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  762. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  763. || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
  764. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
  765. mode = qpnp_regulator_common_get_mode(rdev);
  766. mode_label = mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM";
  767. }
  768. if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  769. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  770. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
  771. en_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE];
  772. pc_enable_label[0] =
  773. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK ? '3' : '_';
  774. pc_enable_label[1] =
  775. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK ? '2' : '_';
  776. pc_enable_label[2] =
  777. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK ? '1' : '_';
  778. pc_enable_label[3] =
  779. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK ? '0' : '_';
  780. }
  781. switch (type) {
  782. case QPNP_REGULATOR_LOGICAL_TYPE_SMPS:
  783. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  784. pc_mode_label[0] =
  785. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  786. pc_mode_label[1] =
  787. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  788. pc_mode_label[2] =
  789. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
  790. pc_mode_label[3] =
  791. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
  792. pc_mode_label[4] =
  793. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
  794. pc_mode_label[5] =
  795. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
  796. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
  797. "alt_mode=%s\n",
  798. action_label, vreg->rdesc.name, enable_label, uV,
  799. mode_label, pc_enable_label, pc_mode_label);
  800. break;
  801. case QPNP_REGULATOR_LOGICAL_TYPE_LDO:
  802. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  803. pc_mode_label[0] =
  804. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  805. pc_mode_label[1] =
  806. mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
  807. pc_mode_label[2] =
  808. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  809. pc_mode_label[3] =
  810. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
  811. pc_mode_label[4] =
  812. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
  813. pc_mode_label[5] =
  814. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
  815. pc_mode_label[6] =
  816. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
  817. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, "
  818. "alt_mode=%s\n",
  819. action_label, vreg->rdesc.name, enable_label, uV,
  820. mode_label, pc_enable_label, pc_mode_label);
  821. break;
  822. case QPNP_REGULATOR_LOGICAL_TYPE_VS:
  823. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  824. pc_mode_label[0] =
  825. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  826. pc_mode_label[1] =
  827. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  828. pr_info("%s %-11s: %s, mode=%s, pc_en=%s, alt_mode=%s\n",
  829. action_label, vreg->rdesc.name, enable_label,
  830. mode_label, pc_enable_label, pc_mode_label);
  831. break;
  832. case QPNP_REGULATOR_LOGICAL_TYPE_BOOST:
  833. pr_info("%s %-11s: %s, v=%7d uV\n",
  834. action_label, vreg->rdesc.name, enable_label, uV);
  835. break;
  836. case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS:
  837. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  838. pc_mode_label[0] =
  839. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  840. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
  841. action_label, vreg->rdesc.name, enable_label, uV,
  842. mode_label, pc_mode_label);
  843. break;
  844. default:
  845. break;
  846. }
  847. }
  848. static struct regulator_ops qpnp_smps_ops = {
  849. .enable = qpnp_regulator_common_enable,
  850. .disable = qpnp_regulator_common_disable,
  851. .is_enabled = qpnp_regulator_common_is_enabled,
  852. .set_voltage = qpnp_regulator_common_set_voltage,
  853. .get_voltage = qpnp_regulator_common_get_voltage,
  854. .list_voltage = qpnp_regulator_common_list_voltage,
  855. .set_mode = qpnp_regulator_common_set_mode,
  856. .get_mode = qpnp_regulator_common_get_mode,
  857. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  858. .enable_time = qpnp_regulator_common_enable_time,
  859. };
  860. static struct regulator_ops qpnp_ldo_ops = {
  861. .enable = qpnp_regulator_common_enable,
  862. .disable = qpnp_regulator_common_disable,
  863. .is_enabled = qpnp_regulator_common_is_enabled,
  864. .set_voltage = qpnp_regulator_common_set_voltage,
  865. .get_voltage = qpnp_regulator_common_get_voltage,
  866. .list_voltage = qpnp_regulator_common_list_voltage,
  867. .set_mode = qpnp_regulator_common_set_mode,
  868. .get_mode = qpnp_regulator_common_get_mode,
  869. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  870. .enable_time = qpnp_regulator_common_enable_time,
  871. };
  872. static struct regulator_ops qpnp_vs_ops = {
  873. .enable = qpnp_regulator_vs_enable,
  874. .disable = qpnp_regulator_common_disable,
  875. .is_enabled = qpnp_regulator_common_is_enabled,
  876. .enable_time = qpnp_regulator_common_enable_time,
  877. };
  878. static struct regulator_ops qpnp_boost_ops = {
  879. .enable = qpnp_regulator_common_enable,
  880. .disable = qpnp_regulator_common_disable,
  881. .is_enabled = qpnp_regulator_common_is_enabled,
  882. .set_voltage = qpnp_regulator_boost_set_voltage,
  883. .get_voltage = qpnp_regulator_boost_get_voltage,
  884. .list_voltage = qpnp_regulator_common_list_voltage,
  885. .enable_time = qpnp_regulator_common_enable_time,
  886. };
  887. static struct regulator_ops qpnp_ftsmps_ops = {
  888. .enable = qpnp_regulator_common_enable,
  889. .disable = qpnp_regulator_common_disable,
  890. .is_enabled = qpnp_regulator_common_is_enabled,
  891. .set_voltage = qpnp_regulator_common_set_voltage,
  892. .get_voltage = qpnp_regulator_common_get_voltage,
  893. .list_voltage = qpnp_regulator_common_list_voltage,
  894. .set_mode = qpnp_regulator_common_set_mode,
  895. .get_mode = qpnp_regulator_common_get_mode,
  896. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  897. .enable_time = qpnp_regulator_common_enable_time,
  898. };
  899. /* Maximum possible digital major revision value */
  900. #define INF 0xFF
  901. static const struct qpnp_regulator_mapping supported_regulators[] = {
  902. /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
  903. QPNP_VREG_MAP(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
  904. QPNP_VREG_MAP(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
  905. QPNP_VREG_MAP(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
  906. QPNP_VREG_MAP(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
  907. QPNP_VREG_MAP(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
  908. QPNP_VREG_MAP(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
  909. QPNP_VREG_MAP(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000),
  910. QPNP_VREG_MAP(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000),
  911. QPNP_VREG_MAP(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000),
  912. QPNP_VREG_MAP(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000),
  913. QPNP_VREG_MAP(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
  914. QPNP_VREG_MAP(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
  915. QPNP_VREG_MAP(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
  916. QPNP_VREG_MAP(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
  917. QPNP_VREG_MAP(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
  918. QPNP_VREG_MAP(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000),
  919. QPNP_VREG_MAP(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000),
  920. QPNP_VREG_MAP(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000),
  921. QPNP_VREG_MAP(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000),
  922. QPNP_VREG_MAP(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000),
  923. QPNP_VREG_MAP(VS, LV100, 0, INF, VS, vs, none, 0),
  924. QPNP_VREG_MAP(VS, LV300, 0, INF, VS, vs, none, 0),
  925. QPNP_VREG_MAP(VS, MV300, 0, INF, VS, vs, none, 0),
  926. QPNP_VREG_MAP(VS, MV500, 0, INF, VS, vs, none, 0),
  927. QPNP_VREG_MAP(VS, HDMI, 0, INF, VS, vs, none, 0),
  928. QPNP_VREG_MAP(VS, OTG, 0, INF, VS, vs, none, 0),
  929. QPNP_VREG_MAP(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
  930. QPNP_VREG_MAP(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
  931. };
  932. static int qpnp_regulator_match(struct qpnp_regulator *vreg)
  933. {
  934. const struct qpnp_regulator_mapping *mapping;
  935. struct device_node *node = vreg->spmi_dev->dev.of_node;
  936. int rc, i;
  937. u32 type_reg[2], dig_major_rev;
  938. u8 version[QPNP_COMMON_REG_SUBTYPE - QPNP_COMMON_REG_DIG_MAJOR_REV + 1];
  939. u8 type, subtype;
  940. rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_DIG_MAJOR_REV, version,
  941. ARRAY_SIZE(version));
  942. if (rc) {
  943. vreg_err(vreg, "could not read version registers, rc=%d\n", rc);
  944. return rc;
  945. }
  946. dig_major_rev = version[QPNP_COMMON_REG_DIG_MAJOR_REV
  947. - QPNP_COMMON_REG_DIG_MAJOR_REV];
  948. type = version[QPNP_COMMON_REG_TYPE
  949. - QPNP_COMMON_REG_DIG_MAJOR_REV];
  950. subtype = version[QPNP_COMMON_REG_SUBTYPE
  951. - QPNP_COMMON_REG_DIG_MAJOR_REV];
  952. /*
  953. * Override type and subtype register values if qcom,force-type is
  954. * present in the device tree node.
  955. */
  956. rc = of_property_read_u32_array(node, "qcom,force-type", type_reg, 2);
  957. if (!rc) {
  958. type = type_reg[0];
  959. subtype = type_reg[1];
  960. }
  961. rc = -ENODEV;
  962. for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
  963. mapping = &supported_regulators[i];
  964. if (mapping->type == type && mapping->subtype == subtype
  965. && mapping->revision_min <= dig_major_rev
  966. && mapping->revision_max >= dig_major_rev) {
  967. vreg->logical_type = mapping->logical_type;
  968. vreg->set_points = mapping->set_points;
  969. vreg->hpm_min_load = mapping->hpm_min_load;
  970. vreg->rdesc.ops = mapping->ops;
  971. vreg->rdesc.n_voltages
  972. = mapping->set_points->n_voltages;
  973. rc = 0;
  974. break;
  975. }
  976. }
  977. return rc;
  978. }
  979. static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg,
  980. struct qpnp_regulator_platform_data *pdata)
  981. {
  982. int rc, i;
  983. enum qpnp_regulator_logical_type type;
  984. u8 ctrl_reg[8], reg, mask;
  985. type = vreg->logical_type;
  986. rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
  987. vreg->ctrl_reg, 8);
  988. if (rc) {
  989. vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
  990. return rc;
  991. }
  992. for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++)
  993. ctrl_reg[i] = vreg->ctrl_reg[i];
  994. /* Set up enable pin control. */
  995. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  996. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  997. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
  998. && !(pdata->pin_ctrl_enable
  999. & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
  1000. ctrl_reg[QPNP_COMMON_IDX_ENABLE] &=
  1001. ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
  1002. ctrl_reg[QPNP_COMMON_IDX_ENABLE] |=
  1003. pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
  1004. }
  1005. /* Set up HPM control. */
  1006. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1007. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1008. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
  1009. || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
  1010. && (pdata->hpm_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
  1011. ctrl_reg[QPNP_COMMON_IDX_MODE] &= ~QPNP_COMMON_MODE_HPM_MASK;
  1012. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1013. (pdata->hpm_enable ? QPNP_COMMON_MODE_HPM_MASK : 0);
  1014. }
  1015. /* Set up auto mode control. */
  1016. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1017. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1018. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
  1019. || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
  1020. && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
  1021. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1022. ~QPNP_COMMON_MODE_AUTO_MASK;
  1023. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1024. (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0);
  1025. }
  1026. /* Set up mode pin control. */
  1027. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1028. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO)
  1029. && !(pdata->pin_ctrl_hpm
  1030. & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1031. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1032. ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
  1033. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1034. pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
  1035. }
  1036. if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS
  1037. && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1038. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1039. ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1040. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1041. pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1042. }
  1043. if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1044. && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1045. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1046. ~QPNP_COMMON_MODE_BYPASS_MASK;
  1047. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1048. (pdata->bypass_mode_enable
  1049. ? QPNP_COMMON_MODE_BYPASS_MASK : 0);
  1050. }
  1051. /* Set boost current limit. */
  1052. if (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
  1053. && pdata->boost_current_limit
  1054. != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) {
  1055. reg = pdata->boost_current_limit;
  1056. mask = QPNP_BOOST_CURRENT_LIMIT_MASK;
  1057. rc = qpnp_vreg_masked_read_write(vreg,
  1058. QPNP_BOOST_REG_CURRENT_LIMIT, reg, mask);
  1059. if (rc) {
  1060. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1061. return rc;
  1062. }
  1063. }
  1064. /* Write back any control register values that were modified. */
  1065. rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
  1066. ctrl_reg, vreg->ctrl_reg, 8);
  1067. if (rc) {
  1068. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1069. return rc;
  1070. }
  1071. /* Set pull down. */
  1072. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1073. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1074. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
  1075. && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1076. reg = pdata->pull_down_enable
  1077. ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
  1078. rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_PULL_DOWN, &reg, 1);
  1079. if (rc) {
  1080. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1081. return rc;
  1082. }
  1083. }
  1084. if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
  1085. && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1086. /* FTSMPS has other bits in the pull down control register. */
  1087. reg = pdata->pull_down_enable
  1088. ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
  1089. rc = qpnp_vreg_masked_read_write(vreg,
  1090. QPNP_COMMON_REG_PULL_DOWN, reg,
  1091. QPNP_COMMON_PULL_DOWN_ENABLE_MASK);
  1092. if (rc) {
  1093. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1094. return rc;
  1095. }
  1096. }
  1097. /* Set soft start for LDO. */
  1098. if (type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1099. && pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1100. reg = pdata->soft_start_enable
  1101. ? QPNP_LDO_SOFT_START_ENABLE_MASK : 0;
  1102. rc = qpnp_vreg_write(vreg, QPNP_LDO_REG_SOFT_START, &reg, 1);
  1103. if (rc) {
  1104. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1105. return rc;
  1106. }
  1107. }
  1108. /* Set soft start strength and over current protection for VS. */
  1109. if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
  1110. reg = 0;
  1111. mask = 0;
  1112. if (pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1113. reg |= pdata->soft_start_enable
  1114. ? QPNP_VS_SOFT_START_ENABLE_MASK : 0;
  1115. mask |= QPNP_VS_SOFT_START_ENABLE_MASK;
  1116. }
  1117. if (pdata->vs_soft_start_strength
  1118. != QPNP_VS_SOFT_START_STR_HW_DEFAULT) {
  1119. reg |= pdata->vs_soft_start_strength
  1120. & QPNP_VS_SOFT_START_SEL_MASK;
  1121. mask |= QPNP_VS_SOFT_START_SEL_MASK;
  1122. }
  1123. rc = qpnp_vreg_masked_read_write(vreg, QPNP_VS_REG_SOFT_START,
  1124. reg, mask);
  1125. if (rc) {
  1126. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1127. return rc;
  1128. }
  1129. if (pdata->ocp_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1130. reg = pdata->ocp_enable ? QPNP_VS_OCP_NO_OVERRIDE
  1131. : QPNP_VS_OCP_OVERRIDE;
  1132. rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
  1133. if (rc) {
  1134. vreg_err(vreg, "spmi write failed, rc=%d\n",
  1135. rc);
  1136. return rc;
  1137. }
  1138. }
  1139. }
  1140. return rc;
  1141. }
  1142. /* Fill in pdata elements based on values found in device tree. */
  1143. static int qpnp_regulator_get_dt_config(struct spmi_device *spmi,
  1144. struct qpnp_regulator_platform_data *pdata)
  1145. {
  1146. struct resource *res;
  1147. struct device_node *node = spmi->dev.of_node;
  1148. int rc = 0;
  1149. pdata->init_data.constraints.input_uV
  1150. = pdata->init_data.constraints.max_uV;
  1151. res = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
  1152. if (!res) {
  1153. dev_err(&spmi->dev, "%s: node is missing base address\n",
  1154. __func__);
  1155. return -EINVAL;
  1156. }
  1157. pdata->base_addr = res->start;
  1158. /* OCP IRQ is optional so ignore get errors. */
  1159. pdata->ocp_irq = spmi_get_irq_byname(spmi, NULL, "ocp");
  1160. if (pdata->ocp_irq < 0)
  1161. pdata->ocp_irq = 0;
  1162. /*
  1163. * Initialize configuration parameters to use hardware default in case
  1164. * no value is specified via device tree.
  1165. */
  1166. pdata->auto_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1167. pdata->bypass_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1168. pdata->ocp_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1169. pdata->pull_down_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1170. pdata->soft_start_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1171. pdata->boost_current_limit = QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT;
  1172. pdata->pin_ctrl_enable = QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
  1173. pdata->pin_ctrl_hpm = QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
  1174. pdata->vs_soft_start_strength = QPNP_VS_SOFT_START_STR_HW_DEFAULT;
  1175. pdata->hpm_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1176. /* These bindings are optional, so it is okay if they are not found. */
  1177. of_property_read_u32(node, "qcom,auto-mode-enable",
  1178. &pdata->auto_mode_enable);
  1179. of_property_read_u32(node, "qcom,bypass-mode-enable",
  1180. &pdata->bypass_mode_enable);
  1181. of_property_read_u32(node, "qcom,ocp-enable", &pdata->ocp_enable);
  1182. of_property_read_u32(node, "qcom,ocp-max-retries",
  1183. &pdata->ocp_max_retries);
  1184. of_property_read_u32(node, "qcom,ocp-retry-delay",
  1185. &pdata->ocp_retry_delay_ms);
  1186. of_property_read_u32(node, "qcom,pull-down-enable",
  1187. &pdata->pull_down_enable);
  1188. of_property_read_u32(node, "qcom,soft-start-enable",
  1189. &pdata->soft_start_enable);
  1190. of_property_read_u32(node, "qcom,boost-current-limit",
  1191. &pdata->boost_current_limit);
  1192. of_property_read_u32(node, "qcom,pin-ctrl-enable",
  1193. &pdata->pin_ctrl_enable);
  1194. of_property_read_u32(node, "qcom,pin-ctrl-hpm", &pdata->pin_ctrl_hpm);
  1195. of_property_read_u32(node, "qcom,hpm-enable", &pdata->hpm_enable);
  1196. of_property_read_u32(node, "qcom,vs-soft-start-strength",
  1197. &pdata->vs_soft_start_strength);
  1198. of_property_read_u32(node, "qcom,system-load", &pdata->system_load);
  1199. of_property_read_u32(node, "qcom,enable-time", &pdata->enable_time);
  1200. return rc;
  1201. }
  1202. static struct of_device_id spmi_match_table[];
  1203. #define MAX_NAME_LEN 127
  1204. static int __devinit qpnp_regulator_probe(struct spmi_device *spmi)
  1205. {
  1206. struct qpnp_regulator_platform_data *pdata;
  1207. struct qpnp_regulator *vreg;
  1208. struct regulator_desc *rdesc;
  1209. struct qpnp_regulator_platform_data of_pdata;
  1210. struct regulator_init_data *init_data;
  1211. char *reg_name;
  1212. int rc;
  1213. bool is_dt;
  1214. vreg = kzalloc(sizeof(struct qpnp_regulator), GFP_KERNEL);
  1215. if (!vreg) {
  1216. dev_err(&spmi->dev, "%s: Can't allocate qpnp_regulator\n",
  1217. __func__);
  1218. return -ENOMEM;
  1219. }
  1220. is_dt = of_match_device(spmi_match_table, &spmi->dev);
  1221. /* Check if device tree is in use. */
  1222. if (is_dt) {
  1223. init_data = of_get_regulator_init_data(&spmi->dev,
  1224. spmi->dev.of_node);
  1225. if (!init_data) {
  1226. dev_err(&spmi->dev, "%s: unable to allocate memory\n",
  1227. __func__);
  1228. kfree(vreg);
  1229. return -ENOMEM;
  1230. }
  1231. memset(&of_pdata, 0,
  1232. sizeof(struct qpnp_regulator_platform_data));
  1233. memcpy(&of_pdata.init_data, init_data,
  1234. sizeof(struct regulator_init_data));
  1235. if (of_get_property(spmi->dev.of_node, "parent-supply", NULL))
  1236. of_pdata.init_data.supply_regulator = "parent";
  1237. rc = qpnp_regulator_get_dt_config(spmi, &of_pdata);
  1238. if (rc) {
  1239. dev_err(&spmi->dev, "%s: DT parsing failed, rc=%d\n",
  1240. __func__, rc);
  1241. kfree(vreg);
  1242. return -ENOMEM;
  1243. }
  1244. pdata = &of_pdata;
  1245. } else {
  1246. pdata = spmi->dev.platform_data;
  1247. }
  1248. if (pdata == NULL) {
  1249. dev_err(&spmi->dev, "%s: no platform data specified\n",
  1250. __func__);
  1251. kfree(vreg);
  1252. return -EINVAL;
  1253. }
  1254. vreg->spmi_dev = spmi;
  1255. vreg->prev_write_count = -1;
  1256. vreg->write_count = 0;
  1257. vreg->base_addr = pdata->base_addr;
  1258. vreg->enable_time = pdata->enable_time;
  1259. vreg->system_load = pdata->system_load;
  1260. vreg->ocp_enable = pdata->ocp_enable;
  1261. vreg->ocp_irq = pdata->ocp_irq;
  1262. vreg->ocp_max_retries = pdata->ocp_max_retries;
  1263. vreg->ocp_retry_delay_ms = pdata->ocp_retry_delay_ms;
  1264. if (vreg->ocp_max_retries == 0)
  1265. vreg->ocp_max_retries = QPNP_VS_OCP_DEFAULT_MAX_RETRIES;
  1266. if (vreg->ocp_retry_delay_ms == 0)
  1267. vreg->ocp_retry_delay_ms = QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS;
  1268. rdesc = &vreg->rdesc;
  1269. rdesc->id = spmi->ctrl->nr;
  1270. rdesc->owner = THIS_MODULE;
  1271. rdesc->type = REGULATOR_VOLTAGE;
  1272. reg_name = kzalloc(strnlen(pdata->init_data.constraints.name,
  1273. MAX_NAME_LEN) + 1, GFP_KERNEL);
  1274. if (!reg_name) {
  1275. dev_err(&spmi->dev, "%s: Can't allocate regulator name\n",
  1276. __func__);
  1277. kfree(vreg);
  1278. return -ENOMEM;
  1279. }
  1280. strlcpy(reg_name, pdata->init_data.constraints.name,
  1281. strnlen(pdata->init_data.constraints.name, MAX_NAME_LEN) + 1);
  1282. rdesc->name = reg_name;
  1283. dev_set_drvdata(&spmi->dev, vreg);
  1284. rc = qpnp_regulator_match(vreg);
  1285. if (rc) {
  1286. vreg_err(vreg, "regulator type unknown, rc=%d\n", rc);
  1287. goto bail;
  1288. }
  1289. if (is_dt && rdesc->ops) {
  1290. /* Fill in ops and mode masks when using device tree. */
  1291. if (rdesc->ops->enable)
  1292. pdata->init_data.constraints.valid_ops_mask
  1293. |= REGULATOR_CHANGE_STATUS;
  1294. if (rdesc->ops->get_voltage)
  1295. pdata->init_data.constraints.valid_ops_mask
  1296. |= REGULATOR_CHANGE_VOLTAGE;
  1297. if (rdesc->ops->get_mode) {
  1298. pdata->init_data.constraints.valid_ops_mask
  1299. |= REGULATOR_CHANGE_MODE
  1300. | REGULATOR_CHANGE_DRMS;
  1301. pdata->init_data.constraints.valid_modes_mask
  1302. = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
  1303. }
  1304. }
  1305. rc = qpnp_regulator_init_registers(vreg, pdata);
  1306. if (rc) {
  1307. vreg_err(vreg, "common initialization failed, rc=%d\n", rc);
  1308. goto bail;
  1309. }
  1310. if (vreg->logical_type != QPNP_REGULATOR_LOGICAL_TYPE_VS)
  1311. vreg->ocp_irq = 0;
  1312. if (vreg->ocp_irq) {
  1313. rc = devm_request_irq(&spmi->dev, vreg->ocp_irq,
  1314. qpnp_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
  1315. vreg);
  1316. if (rc < 0) {
  1317. vreg_err(vreg, "failed to request irq %d, rc=%d\n",
  1318. vreg->ocp_irq, rc);
  1319. goto bail;
  1320. }
  1321. INIT_DELAYED_WORK(&vreg->ocp_work, qpnp_regulator_vs_ocp_work);
  1322. }
  1323. vreg->rdev = regulator_register(rdesc, &spmi->dev,
  1324. &(pdata->init_data), vreg, spmi->dev.of_node);
  1325. if (IS_ERR(vreg->rdev)) {
  1326. rc = PTR_ERR(vreg->rdev);
  1327. if (rc != -EPROBE_DEFER)
  1328. vreg_err(vreg, "regulator_register failed, rc=%d\n",
  1329. rc);
  1330. goto cancel_ocp_work;
  1331. }
  1332. qpnp_vreg_show_state(vreg->rdev, QPNP_REGULATOR_ACTION_INIT);
  1333. return 0;
  1334. cancel_ocp_work:
  1335. if (vreg->ocp_irq)
  1336. cancel_delayed_work_sync(&vreg->ocp_work);
  1337. bail:
  1338. if (rc && rc != -EPROBE_DEFER)
  1339. vreg_err(vreg, "probe failed, rc=%d\n", rc);
  1340. kfree(vreg->rdesc.name);
  1341. kfree(vreg);
  1342. return rc;
  1343. }
  1344. static int __devexit qpnp_regulator_remove(struct spmi_device *spmi)
  1345. {
  1346. struct qpnp_regulator *vreg;
  1347. vreg = dev_get_drvdata(&spmi->dev);
  1348. dev_set_drvdata(&spmi->dev, NULL);
  1349. if (vreg) {
  1350. regulator_unregister(vreg->rdev);
  1351. if (vreg->ocp_irq)
  1352. cancel_delayed_work_sync(&vreg->ocp_work);
  1353. kfree(vreg->rdesc.name);
  1354. kfree(vreg);
  1355. }
  1356. return 0;
  1357. }
  1358. static struct of_device_id spmi_match_table[] = {
  1359. { .compatible = QPNP_REGULATOR_DRIVER_NAME, },
  1360. {}
  1361. };
  1362. static const struct spmi_device_id qpnp_regulator_id[] = {
  1363. { QPNP_REGULATOR_DRIVER_NAME, 0 },
  1364. { }
  1365. };
  1366. MODULE_DEVICE_TABLE(spmi, qpnp_regulator_id);
  1367. static struct spmi_driver qpnp_regulator_driver = {
  1368. .driver = {
  1369. .name = QPNP_REGULATOR_DRIVER_NAME,
  1370. .of_match_table = spmi_match_table,
  1371. .owner = THIS_MODULE,
  1372. },
  1373. .probe = qpnp_regulator_probe,
  1374. .remove = __devexit_p(qpnp_regulator_remove),
  1375. .id_table = qpnp_regulator_id,
  1376. };
  1377. /*
  1378. * Pre-compute the number of set points available for each regulator type to
  1379. * avoid unnecessary calculations later in runtime.
  1380. */
  1381. static void qpnp_regulator_set_point_init(void)
  1382. {
  1383. struct qpnp_voltage_set_points **set_points;
  1384. int i, j, temp;
  1385. set_points = all_set_points;
  1386. for (i = 0; i < ARRAY_SIZE(all_set_points); i++) {
  1387. temp = 0;
  1388. for (j = 0; j < all_set_points[i]->count; j++) {
  1389. all_set_points[i]->range[j].n_voltages
  1390. = (all_set_points[i]->range[j].max_uV
  1391. - all_set_points[i]->range[j].set_point_min_uV)
  1392. / all_set_points[i]->range[j].step_uV + 1;
  1393. temp += all_set_points[i]->range[j].n_voltages;
  1394. }
  1395. all_set_points[i]->n_voltages = temp;
  1396. }
  1397. }
  1398. /**
  1399. * qpnp_regulator_init() - register spmi driver for qpnp-regulator
  1400. *
  1401. * This initialization function should be called in systems in which driver
  1402. * registration ordering must be controlled precisely.
  1403. */
  1404. int __init qpnp_regulator_init(void)
  1405. {
  1406. static bool has_registered;
  1407. if (has_registered)
  1408. return 0;
  1409. else
  1410. has_registered = true;
  1411. qpnp_regulator_set_point_init();
  1412. return spmi_driver_register(&qpnp_regulator_driver);
  1413. }
  1414. EXPORT_SYMBOL(qpnp_regulator_init);
  1415. static void __exit qpnp_regulator_exit(void)
  1416. {
  1417. spmi_driver_unregister(&qpnp_regulator_driver);
  1418. }
  1419. MODULE_DESCRIPTION("QPNP PMIC regulator driver");
  1420. MODULE_LICENSE("GPL v2");
  1421. arch_initcall(qpnp_regulator_init);
  1422. module_exit(qpnp_regulator_exit);