qpnp-pwm.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. /* Copyright (c) 2012-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. /*
  13. * Qualcomm QPNP Pulse Width Modulation (PWM) driver
  14. *
  15. * The HW module is also called LPG (Light Pattern Generator).
  16. */
  17. #define pr_fmt(fmt) "%s: " fmt, __func__
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/spmi.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/radix-tree.h>
  25. #include <linux/qpnp/pwm.h>
  26. #define QPNP_LPG_DRIVER_NAME "qcom,qpnp-pwm"
  27. #define QPNP_LPG_CHANNEL_BASE "qpnp-lpg-channel-base"
  28. #define QPNP_LPG_LUT_BASE "qpnp-lpg-lut-base"
  29. #define QPNP_PWM_MODE_ONLY_SUB_TYPE 0x0B
  30. /* LPG Control for LPG_PATTERN_CONFIG */
  31. #define QPNP_RAMP_DIRECTION_SHIFT 4
  32. #define QPNP_RAMP_DIRECTION_MASK 0x10
  33. #define QPNP_PATTERN_REPEAT_SHIFT 3
  34. #define QPNP_PATTERN_REPEAT_MASK 0x08
  35. #define QPNP_RAMP_TOGGLE_SHIFT 2
  36. #define QPNP_RAMP_TOGGLE_MASK 0x04
  37. #define QPNP_EN_PAUSE_HI_SHIFT 1
  38. #define QPNP_EN_PAUSE_HI_MASK 0x02
  39. #define QPNP_EN_PAUSE_LO_MASK 0x01
  40. /* LPG Control for LPG_PWM_SIZE_CLK */
  41. #define QPNP_PWM_SIZE_SHIFT_SUB_TYPE 2
  42. #define QPNP_PWM_SIZE_MASK_SUB_TYPE 0x4
  43. #define QPNP_PWM_FREQ_CLK_SELECT_MASK_SUB_TYPE 0x03
  44. #define QPNP_PWM_SIZE_9_BIT_SUB_TYPE 0x01
  45. #define QPNP_SET_PWM_CLK_SUB_TYPE(val, clk, pwm_size) \
  46. do { \
  47. val = (clk + 1) & QPNP_PWM_FREQ_CLK_SELECT_MASK_SUB_TYPE; \
  48. val |= (((pwm_size > 6 ? QPNP_PWM_SIZE_9_BIT_SUB_TYPE : 0) << \
  49. QPNP_PWM_SIZE_SHIFT_SUB_TYPE) & QPNP_PWM_SIZE_MASK_SUB_TYPE); \
  50. } while (0)
  51. #define QPNP_GET_PWM_SIZE_SUB_TYPE(reg) ((reg & QPNP_PWM_SIZE_MASK_SUB_TYPE) \
  52. >> QPNP_PWM_SIZE_SHIFT_SUB_TYPE)
  53. #define QPNP_PWM_SIZE_SHIFT 4
  54. #define QPNP_PWM_SIZE_MASK 0x30
  55. #define QPNP_PWM_FREQ_CLK_SELECT_MASK 0x03
  56. #define QPNP_MIN_PWM_BIT_SIZE 6
  57. #define QPNP_MAX_PWM_BIT_SIZE 9
  58. #define QPNP_SET_PWM_CLK(val, clk, pwm_size) \
  59. do { \
  60. val = (clk + 1) & QPNP_PWM_FREQ_CLK_SELECT_MASK; \
  61. val |= (((pwm_size - QPNP_MIN_PWM_BIT_SIZE) << \
  62. QPNP_PWM_SIZE_SHIFT) & QPNP_PWM_SIZE_MASK); \
  63. } while (0)
  64. #define QPNP_GET_PWM_SIZE(reg) ((reg & QPNP_PWM_SIZE_MASK) \
  65. >> QPNP_PWM_SIZE_SHIFT)
  66. /* LPG Control for LPG_PWM_FREQ_PREDIV_CLK */
  67. #define QPNP_PWM_FREQ_PRE_DIVIDE_SHIFT 5
  68. #define QPNP_PWM_FREQ_PRE_DIVIDE_MASK 0x60
  69. #define QPNP_PWM_FREQ_EXP_MASK 0x07
  70. #define QPNP_SET_PWM_FREQ_PREDIV(val, pre_div, pre_div_exp) \
  71. do { \
  72. val = (pre_div << QPNP_PWM_FREQ_PRE_DIVIDE_SHIFT) & \
  73. QPNP_PWM_FREQ_PRE_DIVIDE_MASK; \
  74. val |= (pre_div_exp & QPNP_PWM_FREQ_EXP_MASK); \
  75. } while (0)
  76. /* LPG Control for LPG_PWM_TYPE_CONFIG */
  77. #define QPNP_EN_GLITCH_REMOVAL_SHIFT 5
  78. #define QPNP_EN_GLITCH_REMOVAL_MASK 0x20
  79. #define QPNP_EN_FULL_SCALE_SHIFT 3
  80. #define QPNP_EN_FULL_SCALE_MASK 0x08
  81. #define QPNP_EN_PHASE_STAGGER_SHIFT 2
  82. #define QPNP_EN_PHASE_STAGGER_MASK 0x04
  83. #define QPNP_PHASE_STAGGER_MASK 0x03
  84. /* LPG Control for PWM_VALUE_LSB */
  85. #define QPNP_PWM_VALUE_LSB_MASK 0xFF
  86. /* LPG Control for PWM_VALUE_MSB */
  87. #define QPNP_PWM_VALUE_MSB_SHIFT 8
  88. #define QPNP_PWM_VALUE_MSB_MASK 0x01
  89. /* LPG Control for ENABLE_CONTROL */
  90. #define QPNP_EN_PWM_HIGH_SHIFT 7
  91. #define QPNP_EN_PWM_HIGH_MASK 0x80
  92. #define QPNP_EN_PWM_LO_SHIFT 6
  93. #define QPNP_EN_PWM_LO_MASK 0x40
  94. #define QPNP_EN_PWM_OUTPUT_SHIFT 5
  95. #define QPNP_EN_PWM_OUTPUT_MASK 0x20
  96. #define QPNP_PWM_SRC_SELECT_SHIFT 2
  97. #define QPNP_PWM_SRC_SELECT_MASK 0x04
  98. #define QPNP_PWM_EN_RAMP_GEN_SHIFT 1
  99. #define QPNP_PWM_EN_RAMP_GEN_MASK 0x02
  100. #define QPNP_ENABLE_PWM(value) \
  101. (value |= (1 << QPNP_EN_PWM_OUTPUT_SHIFT) & QPNP_EN_PWM_OUTPUT_MASK)
  102. #define QPNP_DISABLE_PWM(value) (value &= ~QPNP_EN_PWM_OUTPUT_MASK)
  103. /* LPG Control for PWM_SYNC */
  104. #define QPNP_PWM_SYNC_VALUE 0x01
  105. #define QPNP_PWM_SYNC_MASK 0x01
  106. /* LPG Control for RAMP_CONTROL */
  107. #define QPNP_RAMP_START_MASK 0x01
  108. #define QPNP_RAMP_CONTROL_SHIFT 8
  109. #define QPNP_ENABLE_LUT_V0(value) (value |= QPNP_RAMP_START_MASK)
  110. #define QPNP_DISABLE_LUT_V0(value) (value &= ~QPNP_RAMP_START_MASK)
  111. #define QPNP_ENABLE_LUT_V1(value, id) \
  112. do { \
  113. (id < 8) ? (value |= BIT(id)) : \
  114. (value |= (BIT(id) >> QPNP_RAMP_CONTROL_SHIFT)); \
  115. } while (0)
  116. /* LPG Control for RAMP_STEP_DURATION_LSB */
  117. #define QPNP_RAMP_STEP_DURATION_LSB_MASK 0xFF
  118. /* LPG Control for RAMP_STEP_DURATION_MSB */
  119. #define QPNP_RAMP_STEP_DURATION_MSB_SHIFT 8
  120. #define QPNP_RAMP_STEP_DURATION_MSB_MASK 0x01
  121. #define QPNP_PWM_1KHZ 1024
  122. #define QPNP_GET_RAMP_STEP_DURATION(ramp_time_ms) \
  123. ((ramp_time_ms * QPNP_PWM_1KHZ) / 1000)
  124. /* LPG Control for PAUSE_HI_MULTIPLIER_LSB */
  125. #define QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK 0xFF
  126. /* LPG Control for PAUSE_HI_MULTIPLIER_MSB */
  127. #define QPNP_PAUSE_HI_MULTIPLIER_MSB_SHIFT 8
  128. #define QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK 0x1F
  129. /* LPG Control for PAUSE_LO_MULTIPLIER_LSB */
  130. #define QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK 0xFF
  131. /* LPG Control for PAUSE_LO_MULTIPLIER_MSB */
  132. #define QPNP_PAUSE_LO_MULTIPLIER_MSB_SHIFT 8
  133. #define QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK 0x1F
  134. /* LPG Control for HI_INDEX */
  135. #define QPNP_HI_INDEX_MASK 0x3F
  136. /* LPG Control for LO_INDEX */
  137. #define QPNP_LO_INDEX_MASK 0x3F
  138. #define NUM_CLOCKS 3
  139. #define QPNP_PWM_M_MAX 7
  140. #define NSEC_1024HZ (NSEC_PER_SEC / 1024)
  141. #define NSEC_32768HZ (NSEC_PER_SEC / 32768)
  142. #define NSEC_19P2MHZ (NSEC_PER_SEC / 19200000)
  143. #define NUM_LPG_PRE_DIVIDE 4
  144. #define PRE_DIVIDE_1 1
  145. #define PRE_DIVIDE_3 3
  146. #define PRE_DIVIDE_5 5
  147. #define PRE_DIVIDE_6 6
  148. #define SPMI_LPG_REG_BASE_OFFSET 0x40
  149. #define SPMI_LPG_REVISION2_OFFSET 0x1
  150. #define SPMI_LPG_REV1_RAMP_CONTROL_OFFSET 0x86
  151. #define SPMI_LPG_SUB_TYPE_OFFSET 0x5
  152. #define SPMI_LPG_PWM_SYNC 0x7
  153. #define SPMI_LPG_REG_ADDR(b, n) (b + SPMI_LPG_REG_BASE_OFFSET + (n))
  154. #define SPMI_MAX_BUF_LEN 8
  155. #define QPNP_GPLED_LPG_CHANNEL_RANGE_START 8
  156. #define QPNP_GPLED_LPG_CHANNEL_RANGE_END 11
  157. #define qpnp_check_gpled_lpg_channel(id) \
  158. (id >= QPNP_GPLED_LPG_CHANNEL_RANGE_START && \
  159. id <= QPNP_GPLED_LPG_CHANNEL_RANGE_END)
  160. #define QPNP_PWM_LUT_NOT_SUPPORTED 0x1
  161. /* Supported PWM sizes */
  162. #define QPNP_PWM_SIZE_6_BIT 6
  163. #define QPNP_PWM_SIZE_7_BIT 7
  164. #define QPNP_PWM_SIZE_8_BIT 8
  165. #define QPNP_PWM_SIZE_9_BIT 9
  166. /* Supported time levels */
  167. enum time_level {
  168. LVL_NSEC,
  169. LVL_USEC,
  170. };
  171. /* LPG revisions */
  172. enum qpnp_lpg_revision {
  173. QPNP_LPG_REVISION_0 = 0x0,
  174. QPNP_LPG_REVISION_1 = 0x1,
  175. };
  176. /* LPG LUT MODE STATE */
  177. enum qpnp_lut_state {
  178. QPNP_LUT_ENABLE = 0x0,
  179. QPNP_LUT_DISABLE = 0x1,
  180. };
  181. /* PWM MODE STATE */
  182. enum qpnp_pwm_state {
  183. QPNP_PWM_ENABLE = 0x0,
  184. QPNP_PWM_DISABLE = 0x1,
  185. };
  186. /* SPMI LPG registers */
  187. enum qpnp_lpg_registers_list {
  188. QPNP_LPG_PATTERN_CONFIG,
  189. QPNP_LPG_PWM_SIZE_CLK,
  190. QPNP_LPG_PWM_FREQ_PREDIV_CLK,
  191. QPNP_LPG_PWM_TYPE_CONFIG,
  192. QPNP_PWM_VALUE_LSB,
  193. QPNP_PWM_VALUE_MSB,
  194. QPNP_ENABLE_CONTROL,
  195. QPNP_RAMP_CONTROL,
  196. QPNP_RAMP_STEP_DURATION_LSB = QPNP_RAMP_CONTROL + 9,
  197. QPNP_RAMP_STEP_DURATION_MSB,
  198. QPNP_PAUSE_HI_MULTIPLIER_LSB,
  199. QPNP_PAUSE_HI_MULTIPLIER_MSB,
  200. QPNP_PAUSE_LO_MULTIPLIER_LSB,
  201. QPNP_PAUSE_LO_MULTIPLIER_MSB,
  202. QPNP_HI_INDEX,
  203. QPNP_LO_INDEX,
  204. QPNP_TOTAL_LPG_SPMI_REGISTERS
  205. };
  206. /*
  207. * Formula from HSID,
  208. * pause_time (hi/lo) = (pause_cnt- 1)*(ramp_ms)
  209. * OR,
  210. * pause_cnt = (pause_time / ramp_ms) + 1
  211. */
  212. #define QPNP_SET_PAUSE_CNT(to_pause_cnt, from_pause, ramp_ms) \
  213. (to_pause_cnt = (from_pause / (ramp_ms ? ramp_ms : 1)) + 1)
  214. static unsigned int pt_t[NUM_LPG_PRE_DIVIDE][NUM_CLOCKS] = {
  215. { PRE_DIVIDE_1 * NSEC_1024HZ,
  216. PRE_DIVIDE_1 * NSEC_32768HZ,
  217. PRE_DIVIDE_1 * NSEC_19P2MHZ,
  218. },
  219. { PRE_DIVIDE_3 * NSEC_1024HZ,
  220. PRE_DIVIDE_3 * NSEC_32768HZ,
  221. PRE_DIVIDE_3 * NSEC_19P2MHZ,
  222. },
  223. { PRE_DIVIDE_5 * NSEC_1024HZ,
  224. PRE_DIVIDE_5 * NSEC_32768HZ,
  225. PRE_DIVIDE_5 * NSEC_19P2MHZ,
  226. },
  227. { PRE_DIVIDE_6 * NSEC_1024HZ,
  228. PRE_DIVIDE_6 * NSEC_32768HZ,
  229. PRE_DIVIDE_6 * NSEC_19P2MHZ,
  230. },
  231. };
  232. static RADIX_TREE(lpg_dev_tree, GFP_KERNEL);
  233. struct qpnp_lut_config {
  234. u8 *duty_pct_list;
  235. int list_len;
  236. int lo_index;
  237. int hi_index;
  238. int lut_pause_hi_cnt;
  239. int lut_pause_lo_cnt;
  240. int ramp_step_ms;
  241. bool ramp_direction;
  242. bool pattern_repeat;
  243. bool ramp_toggle;
  244. bool enable_pause_hi;
  245. bool enable_pause_lo;
  246. };
  247. struct qpnp_lpg_config {
  248. struct qpnp_lut_config lut_config;
  249. u16 base_addr;
  250. u16 lut_base_addr;
  251. u16 lut_size;
  252. };
  253. struct qpnp_pwm_config {
  254. int channel_id;
  255. bool in_use;
  256. const char *lable;
  257. int pwm_value;
  258. int pwm_period; /* in microseconds */
  259. int pwm_duty; /* in microseconds */
  260. struct pwm_period_config period;
  261. int force_pwm_size;
  262. };
  263. /* Public facing structure */
  264. struct pwm_device {
  265. struct qpnp_lpg_chip *chip;
  266. struct qpnp_pwm_config pwm_config;
  267. };
  268. struct qpnp_lpg_chip {
  269. struct spmi_device *spmi_dev;
  270. struct pwm_device pwm_dev;
  271. spinlock_t lpg_lock;
  272. struct qpnp_lpg_config lpg_config;
  273. u8 qpnp_lpg_registers[QPNP_TOTAL_LPG_SPMI_REGISTERS];
  274. enum qpnp_lpg_revision revision;
  275. u8 sub_type;
  276. u32 flags;
  277. };
  278. /* Internal functions */
  279. static inline void qpnp_set_pattern_config(u8 *val,
  280. struct qpnp_lut_config *lut_config)
  281. {
  282. *val = lut_config->enable_pause_lo & QPNP_EN_PAUSE_LO_MASK;
  283. *val |= (lut_config->enable_pause_hi << QPNP_EN_PAUSE_HI_SHIFT) &
  284. QPNP_EN_PAUSE_HI_MASK;
  285. *val |= (lut_config->ramp_toggle << QPNP_RAMP_TOGGLE_SHIFT) &
  286. QPNP_RAMP_TOGGLE_MASK;
  287. *val |= (lut_config->pattern_repeat << QPNP_PATTERN_REPEAT_SHIFT) &
  288. QPNP_PATTERN_REPEAT_MASK;
  289. *val |= (lut_config->ramp_direction << QPNP_RAMP_DIRECTION_SHIFT) &
  290. QPNP_RAMP_DIRECTION_MASK;
  291. }
  292. static inline void qpnp_set_pwm_type_config(u8 *val, bool glitch,
  293. bool full_scale, bool en_phase, bool phase)
  294. {
  295. *val = phase;
  296. *val |= (en_phase << QPNP_EN_PHASE_STAGGER_SHIFT) &
  297. QPNP_EN_PHASE_STAGGER_MASK;
  298. *val |= (full_scale << QPNP_EN_FULL_SCALE_SHIFT) &
  299. QPNP_EN_FULL_SCALE_MASK;
  300. *val |= (glitch << QPNP_EN_GLITCH_REMOVAL_SHIFT) &
  301. QPNP_EN_GLITCH_REMOVAL_MASK;
  302. }
  303. static int qpnp_set_control(bool pwm_hi, bool pwm_lo, bool pwm_out,
  304. bool pwm_src, bool ramp_gen)
  305. {
  306. return (ramp_gen << QPNP_PWM_EN_RAMP_GEN_SHIFT)
  307. | (pwm_src << QPNP_PWM_SRC_SELECT_SHIFT)
  308. | (pwm_out << QPNP_EN_PWM_OUTPUT_SHIFT)
  309. | (pwm_lo << QPNP_EN_PWM_LO_SHIFT)
  310. | (pwm_hi << QPNP_EN_PWM_HIGH_SHIFT);
  311. }
  312. #define QPNP_ENABLE_LUT_CONTROL qpnp_set_control(0, 0, 0, 0, 1)
  313. #define QPNP_ENABLE_PWM_CONTROL qpnp_set_control(0, 0, 0, 1, 0)
  314. #define QPNP_ENABLE_PWM_MODE qpnp_set_control(1, 1, 1, 1, 0)
  315. #define QPNP_ENABLE_PWM_MODE_GPLED_CHANNEL qpnp_set_control(1, 1, 1, 1, 1)
  316. #define QPNP_ENABLE_LPG_MODE qpnp_set_control(1, 1, 1, 0, 1)
  317. #define QPNP_DISABLE_PWM_MODE qpnp_set_control(0, 0, 0, 1, 0)
  318. #define QPNP_DISABLE_LPG_MODE qpnp_set_control(0, 0, 0, 0, 1)
  319. #define QPNP_IS_PWM_CONFIG_SELECTED(val) (val & QPNP_PWM_SRC_SELECT_MASK)
  320. #define QPNP_ENABLE_PWM_MODE_ONLY_SUB_TYPE 0x80
  321. #define QPNP_DISABLE_PWM_MODE_ONLY_SUB_TYPE 0x0
  322. #define QPNP_PWM_MODE_ONLY_ENABLE_DISABLE_MASK_SUB_TYPE 0x80
  323. static inline void qpnp_convert_to_lut_flags(int *flags,
  324. struct qpnp_lut_config *l_config)
  325. {
  326. *flags = ((l_config->ramp_direction ? PM_PWM_LUT_RAMP_UP : 0) |
  327. (l_config->pattern_repeat ? PM_PWM_LUT_LOOP : 0)|
  328. (l_config->ramp_toggle ? PM_PWM_LUT_REVERSE : 0) |
  329. (l_config->enable_pause_hi ? PM_PWM_LUT_PAUSE_HI_EN : 0) |
  330. (l_config->enable_pause_lo ? PM_PWM_LUT_PAUSE_LO_EN : 0));
  331. }
  332. static inline void qpnp_set_lut_params(struct lut_params *l_params,
  333. struct qpnp_lut_config *l_config, int s_idx, int size)
  334. {
  335. l_params->start_idx = s_idx;
  336. l_params->idx_len = size;
  337. l_params->lut_pause_hi = l_config->lut_pause_hi_cnt;
  338. l_params->lut_pause_lo = l_config->lut_pause_lo_cnt;
  339. l_params->ramp_step_ms = l_config->ramp_step_ms;
  340. qpnp_convert_to_lut_flags(&l_params->flags, l_config);
  341. }
  342. static void qpnp_lpg_save(u8 *u8p, u8 mask, u8 val)
  343. {
  344. *u8p &= ~mask;
  345. *u8p |= val & mask;
  346. }
  347. static int qpnp_lpg_save_and_write(u8 value, u8 mask, u8 *reg, u16 addr,
  348. u16 size, struct qpnp_lpg_chip *chip)
  349. {
  350. qpnp_lpg_save(reg, mask, value);
  351. return spmi_ext_register_writel(chip->spmi_dev->ctrl,
  352. chip->spmi_dev->sid, addr, reg, size);
  353. }
  354. /*
  355. * PWM Frequency = Clock Frequency / (N * T)
  356. * or
  357. * PWM Period = Clock Period * (N * T)
  358. * where
  359. * N = 2^9 or 2^6 for 9-bit or 6-bit PWM size
  360. * T = Pre-divide * 2^m, where m = 0..7 (exponent)
  361. *
  362. * This is the formula to figure out m for the best pre-divide and clock:
  363. * (PWM Period / N) = (Pre-divide * Clock Period) * 2^m
  364. */
  365. static void qpnp_lpg_calc_period(enum time_level tm_lvl,
  366. unsigned int period_value,
  367. struct pwm_device *pwm)
  368. {
  369. int n, m, clk, div;
  370. int best_m, best_div, best_clk;
  371. unsigned int last_err, cur_err, min_err;
  372. unsigned int tmp_p, period_n;
  373. int id = pwm->pwm_config.channel_id;
  374. int force_pwm_size = pwm->pwm_config.force_pwm_size;
  375. struct qpnp_lpg_chip *chip = pwm->chip;
  376. struct pwm_period_config *period = &pwm->pwm_config.period;
  377. /* PWM Period / N */
  378. if (qpnp_check_gpled_lpg_channel(id))
  379. n = 7;
  380. else
  381. n = 6;
  382. if (tm_lvl == LVL_USEC) {
  383. if (period_value < ((unsigned)(-1) / NSEC_PER_USEC)) {
  384. period_n = (period_value * NSEC_PER_USEC) >> n;
  385. } else {
  386. if (qpnp_check_gpled_lpg_channel(id))
  387. n = 8;
  388. else
  389. n = 9;
  390. period_n = (period_value >> n) * NSEC_PER_USEC;
  391. }
  392. } else {
  393. period_n = period_value >> n;
  394. }
  395. if (force_pwm_size != 0) {
  396. if (n < force_pwm_size)
  397. period_n = period_n >> (force_pwm_size - n);
  398. else
  399. period_n = period_n << (n - force_pwm_size);
  400. n = force_pwm_size;
  401. pr_info("LPG channel '%d' pwm size is forced to=%d\n", id, n);
  402. }
  403. min_err = last_err = (unsigned)(-1);
  404. best_m = 0;
  405. best_clk = 0;
  406. best_div = 0;
  407. for (clk = 0; clk < NUM_CLOCKS; clk++) {
  408. for (div = 0; div < NUM_LPG_PRE_DIVIDE; div++) {
  409. /* period_n = (PWM Period / N) */
  410. /* tmp_p = (Pre-divide * Clock Period) * 2^m */
  411. tmp_p = pt_t[div][clk];
  412. for (m = 0; m <= QPNP_PWM_M_MAX; m++) {
  413. if (period_n > tmp_p)
  414. cur_err = period_n - tmp_p;
  415. else
  416. cur_err = tmp_p - period_n;
  417. if (cur_err < min_err) {
  418. min_err = cur_err;
  419. best_m = m;
  420. best_clk = clk;
  421. best_div = div;
  422. }
  423. if (m && cur_err > last_err)
  424. /* Break for bigger cur_err */
  425. break;
  426. last_err = cur_err;
  427. tmp_p <<= 1;
  428. }
  429. }
  430. }
  431. /* Adapt to optimal pwm size, the higher the resolution the better */
  432. if (!force_pwm_size) {
  433. if (qpnp_check_gpled_lpg_channel(id)) {
  434. if (n == 7 && best_m >= 1) {
  435. n += 1;
  436. best_m -= 1;
  437. }
  438. } else if (n == 6) {
  439. if (best_m >= 3) {
  440. n += 3;
  441. best_m -= 3;
  442. } else if (best_m >= 1 &&
  443. chip->sub_type != QPNP_PWM_MODE_ONLY_SUB_TYPE) {
  444. n += 1;
  445. best_m -= 1;
  446. }
  447. }
  448. }
  449. period->pwm_size = n;
  450. period->clk = best_clk;
  451. period->pre_div = best_div;
  452. period->pre_div_exp = best_m;
  453. }
  454. static void qpnp_lpg_calc_pwm_value(struct pwm_device *pwm,
  455. unsigned int period_value,
  456. unsigned int duty_value)
  457. {
  458. unsigned int max_pwm_value, tmp;
  459. struct qpnp_pwm_config *pwm_config = &pwm->pwm_config;
  460. /* Figure out pwm_value with overflow handling */
  461. tmp = 1 << (sizeof(tmp) * 8 - pwm_config->period.pwm_size);
  462. if (duty_value < tmp) {
  463. tmp = duty_value << pwm_config->period.pwm_size;
  464. pwm_config->pwm_value = tmp / period_value;
  465. } else {
  466. tmp = period_value >> pwm_config->period.pwm_size;
  467. pwm_config->pwm_value = duty_value / tmp;
  468. }
  469. max_pwm_value = (1 << pwm_config->period.pwm_size) - 1;
  470. if (pwm_config->pwm_value > max_pwm_value)
  471. pwm_config->pwm_value = max_pwm_value;
  472. }
  473. static int qpnp_lpg_change_table(struct pwm_device *pwm,
  474. int duty_pct[], int raw_value)
  475. {
  476. unsigned int pwm_value, max_pwm_value;
  477. struct qpnp_lpg_chip *chip = pwm->chip;
  478. struct qpnp_lut_config *lut = &chip->lpg_config.lut_config;
  479. int i, pwm_size, rc = 0;
  480. int burst_size = SPMI_MAX_BUF_LEN;
  481. int list_len = lut->list_len << 1;
  482. int offset = (lut->lo_index << 1) - 2;
  483. pwm_size = QPNP_GET_PWM_SIZE(
  484. chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) +
  485. QPNP_MIN_PWM_BIT_SIZE;
  486. max_pwm_value = (1 << pwm_size) - 1;
  487. if (unlikely(lut->list_len != (lut->hi_index - lut->lo_index + 1))) {
  488. pr_err("LUT internal Data structure corruption detected\n");
  489. pr_err("LUT list size: %d\n", lut->list_len);
  490. pr_err("However, index size is: %d\n",
  491. (lut->hi_index - lut->lo_index + 1));
  492. return -EINVAL;
  493. }
  494. for (i = 0; i < lut->list_len; i++) {
  495. if (raw_value)
  496. pwm_value = duty_pct[i];
  497. else
  498. pwm_value = (duty_pct[i] << pwm_size) / 100;
  499. if (pwm_value > max_pwm_value)
  500. pwm_value = max_pwm_value;
  501. if (qpnp_check_gpled_lpg_channel(pwm->pwm_config.channel_id)) {
  502. lut->duty_pct_list[i] = pwm_value;
  503. } else {
  504. lut->duty_pct_list[i*2] = pwm_value;
  505. lut->duty_pct_list[(i*2)+1] = (pwm_value >>
  506. QPNP_PWM_VALUE_MSB_SHIFT) & QPNP_PWM_VALUE_MSB_MASK;
  507. }
  508. }
  509. /*
  510. * For the Keypad Backlight Lookup Table (KPDBL_LUT),
  511. * offset is lo_index.
  512. */
  513. if (qpnp_check_gpled_lpg_channel(pwm->pwm_config.channel_id))
  514. offset = lut->lo_index;
  515. /* Write with max allowable burst mode, each entry is of two bytes */
  516. for (i = 0; i < list_len; i += burst_size) {
  517. if (i + burst_size >= list_len)
  518. burst_size = list_len - i;
  519. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl,
  520. chip->spmi_dev->sid,
  521. chip->lpg_config.lut_base_addr + offset + i,
  522. lut->duty_pct_list + i, burst_size);
  523. }
  524. return rc;
  525. }
  526. static void qpnp_lpg_save_period(struct pwm_device *pwm)
  527. {
  528. u8 mask, val;
  529. struct qpnp_lpg_chip *chip = pwm->chip;
  530. struct qpnp_pwm_config *pwm_config = &pwm->pwm_config;
  531. if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) {
  532. QPNP_SET_PWM_CLK_SUB_TYPE(val, pwm_config->period.clk,
  533. pwm_config->period.pwm_size);
  534. mask = QPNP_PWM_SIZE_MASK_SUB_TYPE |
  535. QPNP_PWM_FREQ_CLK_SELECT_MASK_SUB_TYPE;
  536. } else {
  537. QPNP_SET_PWM_CLK(val, pwm_config->period.clk,
  538. pwm_config->period.pwm_size);
  539. mask = QPNP_PWM_SIZE_MASK | QPNP_PWM_FREQ_CLK_SELECT_MASK;
  540. }
  541. qpnp_lpg_save(&chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK],
  542. mask, val);
  543. QPNP_SET_PWM_FREQ_PREDIV(val, pwm_config->period.pre_div,
  544. pwm_config->period.pre_div_exp);
  545. mask = QPNP_PWM_FREQ_PRE_DIVIDE_MASK | QPNP_PWM_FREQ_EXP_MASK;
  546. qpnp_lpg_save(&chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK],
  547. mask, val);
  548. }
  549. static int qpnp_lpg_save_pwm_value(struct pwm_device *pwm)
  550. {
  551. unsigned int max_pwm_value;
  552. int pwm_size;
  553. u8 mask, value;
  554. struct qpnp_lpg_chip *chip = pwm->chip;
  555. struct qpnp_pwm_config *pwm_config = &pwm->pwm_config;
  556. struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
  557. int rc;
  558. if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE)
  559. pwm_size = QPNP_GET_PWM_SIZE_SUB_TYPE(
  560. chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) ?
  561. QPNP_MAX_PWM_BIT_SIZE : QPNP_MIN_PWM_BIT_SIZE;
  562. else
  563. pwm_size = QPNP_GET_PWM_SIZE(
  564. chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK]) +
  565. QPNP_MIN_PWM_BIT_SIZE;
  566. max_pwm_value = (1 << pwm_size) - 1;
  567. if (pwm_config->pwm_value > max_pwm_value)
  568. pwm_config->pwm_value = max_pwm_value;
  569. value = pwm_config->pwm_value;
  570. mask = QPNP_PWM_VALUE_LSB_MASK;
  571. rc = qpnp_lpg_save_and_write(value, mask,
  572. &pwm->chip->qpnp_lpg_registers[QPNP_PWM_VALUE_LSB],
  573. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  574. QPNP_PWM_VALUE_LSB), 1, chip);
  575. if (rc)
  576. return rc;
  577. value = (pwm_config->pwm_value >> QPNP_PWM_VALUE_MSB_SHIFT) &
  578. QPNP_PWM_VALUE_MSB_MASK;
  579. mask = QPNP_PWM_VALUE_MSB_MASK;
  580. rc = qpnp_lpg_save_and_write(value, mask,
  581. &pwm->chip->qpnp_lpg_registers[QPNP_PWM_VALUE_MSB],
  582. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  583. QPNP_PWM_VALUE_MSB), 1, chip);
  584. if (rc)
  585. return rc;
  586. if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) {
  587. value = QPNP_PWM_SYNC_VALUE & QPNP_PWM_SYNC_MASK;
  588. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl,
  589. chip->spmi_dev->sid,
  590. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  591. SPMI_LPG_PWM_SYNC), &value, 1);
  592. }
  593. return rc;
  594. }
  595. static int qpnp_lpg_configure_pattern(struct pwm_device *pwm)
  596. {
  597. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  598. struct qpnp_lut_config *lut_config = &lpg_config->lut_config;
  599. struct qpnp_lpg_chip *chip = pwm->chip;
  600. u8 value, mask;
  601. qpnp_set_pattern_config(&value, lut_config);
  602. mask = QPNP_RAMP_DIRECTION_MASK | QPNP_PATTERN_REPEAT_MASK |
  603. QPNP_RAMP_TOGGLE_MASK | QPNP_EN_PAUSE_HI_MASK |
  604. QPNP_EN_PAUSE_LO_MASK;
  605. return qpnp_lpg_save_and_write(value, mask,
  606. &pwm->chip->qpnp_lpg_registers[QPNP_LPG_PATTERN_CONFIG],
  607. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  608. QPNP_LPG_PATTERN_CONFIG), 1, chip);
  609. }
  610. static int qpnp_lpg_configure_pwm(struct pwm_device *pwm)
  611. {
  612. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  613. struct qpnp_lpg_chip *chip = pwm->chip;
  614. int rc;
  615. u8 value, mask;
  616. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  617. SPMI_LPG_REG_ADDR(lpg_config->base_addr, QPNP_LPG_PWM_SIZE_CLK),
  618. &chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK], 1);
  619. if (rc)
  620. return rc;
  621. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  622. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  623. QPNP_LPG_PWM_FREQ_PREDIV_CLK),
  624. &chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK], 1);
  625. if (rc)
  626. return rc;
  627. qpnp_set_pwm_type_config(&value, 1, 0, 0, 0);
  628. mask = QPNP_EN_GLITCH_REMOVAL_MASK | QPNP_EN_FULL_SCALE_MASK |
  629. QPNP_EN_PHASE_STAGGER_MASK | QPNP_PHASE_STAGGER_MASK;
  630. return qpnp_lpg_save_and_write(value, mask,
  631. &pwm->chip->qpnp_lpg_registers[QPNP_LPG_PWM_TYPE_CONFIG],
  632. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  633. QPNP_LPG_PWM_TYPE_CONFIG), 1, chip);
  634. }
  635. static int qpnp_configure_pwm_control(struct pwm_device *pwm)
  636. {
  637. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  638. struct qpnp_lpg_chip *chip = pwm->chip;
  639. u8 value, mask;
  640. if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE)
  641. return 0;
  642. value = QPNP_ENABLE_PWM_CONTROL;
  643. mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
  644. QPNP_EN_PWM_OUTPUT_MASK | QPNP_PWM_SRC_SELECT_MASK |
  645. QPNP_PWM_EN_RAMP_GEN_MASK;
  646. return qpnp_lpg_save_and_write(value, mask,
  647. &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
  648. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  649. QPNP_ENABLE_CONTROL), 1, chip);
  650. }
  651. static int qpnp_configure_lpg_control(struct pwm_device *pwm)
  652. {
  653. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  654. struct qpnp_lpg_chip *chip = pwm->chip;
  655. u8 value, mask;
  656. value = QPNP_ENABLE_LUT_CONTROL;
  657. mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
  658. QPNP_EN_PWM_OUTPUT_MASK | QPNP_PWM_SRC_SELECT_MASK |
  659. QPNP_PWM_EN_RAMP_GEN_MASK;
  660. return qpnp_lpg_save_and_write(value, mask,
  661. &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
  662. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  663. QPNP_ENABLE_CONTROL), 1, chip);
  664. }
  665. static int qpnp_lpg_configure_ramp_step_duration(struct pwm_device *pwm)
  666. {
  667. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  668. struct qpnp_lut_config lut_config = lpg_config->lut_config;
  669. struct qpnp_lpg_chip *chip = pwm->chip;
  670. int rc, value;
  671. u8 val, mask;
  672. value = QPNP_GET_RAMP_STEP_DURATION(lut_config.ramp_step_ms);
  673. val = value & QPNP_RAMP_STEP_DURATION_LSB_MASK;
  674. mask = QPNP_RAMP_STEP_DURATION_LSB_MASK;
  675. rc = qpnp_lpg_save_and_write(val, mask,
  676. &pwm->chip->qpnp_lpg_registers[QPNP_RAMP_STEP_DURATION_LSB],
  677. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  678. QPNP_RAMP_STEP_DURATION_LSB), 1, chip);
  679. if (rc)
  680. return rc;
  681. val = (value >> QPNP_RAMP_STEP_DURATION_MSB_SHIFT) &
  682. QPNP_RAMP_STEP_DURATION_MSB_MASK;
  683. mask = QPNP_RAMP_STEP_DURATION_MSB_MASK;
  684. return qpnp_lpg_save_and_write(val, mask,
  685. &pwm->chip->qpnp_lpg_registers[QPNP_RAMP_STEP_DURATION_MSB],
  686. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  687. QPNP_RAMP_STEP_DURATION_MSB), 1, chip);
  688. }
  689. static int qpnp_lpg_configure_pause(struct pwm_device *pwm)
  690. {
  691. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  692. struct qpnp_lut_config lut_config = lpg_config->lut_config;
  693. struct qpnp_lpg_chip *chip = pwm->chip;
  694. u8 value, mask;
  695. int rc = 0;
  696. if (lut_config.enable_pause_hi) {
  697. value = lut_config.lut_pause_hi_cnt;
  698. mask = QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK;
  699. rc = qpnp_lpg_save_and_write(value, mask,
  700. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_LSB],
  701. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  702. QPNP_PAUSE_HI_MULTIPLIER_LSB), 1, chip);
  703. if (rc)
  704. return rc;
  705. value = (lut_config.lut_pause_hi_cnt >>
  706. QPNP_PAUSE_HI_MULTIPLIER_MSB_SHIFT) &
  707. QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
  708. mask = QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
  709. rc = qpnp_lpg_save_and_write(value, mask,
  710. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_MSB],
  711. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  712. QPNP_PAUSE_HI_MULTIPLIER_MSB), 1, chip);
  713. } else {
  714. value = 0;
  715. mask = QPNP_PAUSE_HI_MULTIPLIER_LSB_MASK;
  716. rc = qpnp_lpg_save_and_write(value, mask,
  717. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_LSB],
  718. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  719. QPNP_PAUSE_HI_MULTIPLIER_LSB), 1, chip);
  720. if (rc)
  721. return rc;
  722. mask = QPNP_PAUSE_HI_MULTIPLIER_MSB_MASK;
  723. rc = qpnp_lpg_save_and_write(value, mask,
  724. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_HI_MULTIPLIER_MSB],
  725. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  726. QPNP_PAUSE_HI_MULTIPLIER_MSB), 1, chip);
  727. if (rc)
  728. return rc;
  729. }
  730. if (lut_config.enable_pause_lo) {
  731. value = lut_config.lut_pause_lo_cnt;
  732. mask = QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK;
  733. rc = qpnp_lpg_save_and_write(value, mask,
  734. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_LSB],
  735. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  736. QPNP_PAUSE_LO_MULTIPLIER_LSB), 1, chip);
  737. if (rc)
  738. return rc;
  739. value = (lut_config.lut_pause_lo_cnt >>
  740. QPNP_PAUSE_LO_MULTIPLIER_MSB_SHIFT) &
  741. QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
  742. mask = QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
  743. rc = qpnp_lpg_save_and_write(value, mask,
  744. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_MSB],
  745. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  746. QPNP_PAUSE_LO_MULTIPLIER_MSB), 1, chip);
  747. } else {
  748. value = 0;
  749. mask = QPNP_PAUSE_LO_MULTIPLIER_LSB_MASK;
  750. rc = qpnp_lpg_save_and_write(value, mask,
  751. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_LSB],
  752. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  753. QPNP_PAUSE_LO_MULTIPLIER_LSB), 1, chip);
  754. if (rc)
  755. return rc;
  756. mask = QPNP_PAUSE_LO_MULTIPLIER_MSB_MASK;
  757. rc = qpnp_lpg_save_and_write(value, mask,
  758. &pwm->chip->qpnp_lpg_registers[QPNP_PAUSE_LO_MULTIPLIER_MSB],
  759. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  760. QPNP_PAUSE_LO_MULTIPLIER_MSB), 1, chip);
  761. return rc;
  762. }
  763. return rc;
  764. }
  765. static int qpnp_lpg_configure_index(struct pwm_device *pwm)
  766. {
  767. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  768. struct qpnp_lut_config lut_config = lpg_config->lut_config;
  769. struct qpnp_lpg_chip *chip = pwm->chip;
  770. u8 value, mask;
  771. int rc = 0;
  772. value = lut_config.hi_index;
  773. mask = QPNP_HI_INDEX_MASK;
  774. rc = qpnp_lpg_save_and_write(value, mask,
  775. &pwm->chip->qpnp_lpg_registers[QPNP_HI_INDEX],
  776. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  777. QPNP_HI_INDEX), 1, chip);
  778. if (rc)
  779. return rc;
  780. value = lut_config.lo_index;
  781. mask = QPNP_LO_INDEX_MASK;
  782. rc = qpnp_lpg_save_and_write(value, mask,
  783. &pwm->chip->qpnp_lpg_registers[QPNP_LO_INDEX],
  784. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  785. QPNP_LO_INDEX), 1, chip);
  786. return rc;
  787. }
  788. static int qpnp_lpg_change_lut(struct pwm_device *pwm)
  789. {
  790. int rc;
  791. rc = qpnp_lpg_configure_pattern(pwm);
  792. if (rc) {
  793. pr_err("Failed to configure LUT pattern");
  794. return rc;
  795. }
  796. rc = qpnp_lpg_configure_pwm(pwm);
  797. if (rc) {
  798. pr_err("Failed to configure LUT pattern");
  799. return rc;
  800. }
  801. rc = qpnp_configure_lpg_control(pwm);
  802. if (rc) {
  803. pr_err("Failed to configure pause registers");
  804. return rc;
  805. }
  806. rc = qpnp_lpg_configure_ramp_step_duration(pwm);
  807. if (rc) {
  808. pr_err("Failed to configure duty time");
  809. return rc;
  810. }
  811. rc = qpnp_lpg_configure_pause(pwm);
  812. if (rc) {
  813. pr_err("Failed to configure pause registers");
  814. return rc;
  815. }
  816. rc = qpnp_lpg_configure_index(pwm);
  817. if (rc) {
  818. pr_err("Failed to configure index registers");
  819. return rc;
  820. }
  821. return rc;
  822. }
  823. static int qpnp_lpg_configure_lut_state(struct pwm_device *pwm,
  824. enum qpnp_lut_state state)
  825. {
  826. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  827. struct qpnp_lpg_chip *chip = pwm->chip;
  828. u8 value1, value2, mask1, mask2;
  829. u8 *reg1, *reg2;
  830. u16 addr, addr1;
  831. int rc;
  832. value1 = pwm->chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL];
  833. reg1 = &pwm->chip->qpnp_lpg_registers[QPNP_RAMP_CONTROL];
  834. reg2 = &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL];
  835. mask2 = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
  836. QPNP_EN_PWM_OUTPUT_MASK | QPNP_PWM_SRC_SELECT_MASK |
  837. QPNP_PWM_EN_RAMP_GEN_MASK;
  838. switch (chip->revision) {
  839. case QPNP_LPG_REVISION_0:
  840. if (state == QPNP_LUT_ENABLE) {
  841. QPNP_ENABLE_LUT_V0(value1);
  842. value2 = QPNP_ENABLE_LPG_MODE;
  843. } else {
  844. QPNP_DISABLE_LUT_V0(value1);
  845. value2 = QPNP_DISABLE_LPG_MODE;
  846. }
  847. mask1 = QPNP_RAMP_START_MASK;
  848. addr1 = SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  849. QPNP_RAMP_CONTROL);
  850. break;
  851. case QPNP_LPG_REVISION_1:
  852. if (state == QPNP_LUT_ENABLE) {
  853. QPNP_ENABLE_LUT_V1(value1, pwm->pwm_config.channel_id);
  854. value2 = QPNP_ENABLE_LPG_MODE;
  855. } else {
  856. value2 = QPNP_DISABLE_LPG_MODE;
  857. }
  858. mask1 = value1;
  859. addr1 = lpg_config->lut_base_addr +
  860. SPMI_LPG_REV1_RAMP_CONTROL_OFFSET;
  861. break;
  862. default:
  863. pr_err("Invalid LPG revision\n");
  864. return -EINVAL;
  865. }
  866. addr = SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  867. QPNP_ENABLE_CONTROL);
  868. rc = qpnp_lpg_save_and_write(value2, mask2, reg2,
  869. addr, 1, chip);
  870. if (rc)
  871. return rc;
  872. if (state == QPNP_LUT_ENABLE || chip->revision == QPNP_LPG_REVISION_0)
  873. rc = qpnp_lpg_save_and_write(value1, mask1, reg1,
  874. addr1, 1, chip);
  875. return rc;
  876. }
  877. static inline int qpnp_enable_pwm_mode(struct qpnp_pwm_config *pwm_conf)
  878. {
  879. if (qpnp_check_gpled_lpg_channel(pwm_conf->channel_id))
  880. return QPNP_ENABLE_PWM_MODE_GPLED_CHANNEL;
  881. return QPNP_ENABLE_PWM_MODE;
  882. }
  883. static int qpnp_lpg_configure_pwm_state(struct pwm_device *pwm,
  884. enum qpnp_pwm_state state)
  885. {
  886. struct qpnp_lpg_config *lpg_config = &pwm->chip->lpg_config;
  887. struct qpnp_lpg_chip *chip = pwm->chip;
  888. u8 value, mask;
  889. int rc;
  890. if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) {
  891. if (state == QPNP_PWM_ENABLE)
  892. value = QPNP_ENABLE_PWM_MODE_ONLY_SUB_TYPE;
  893. else
  894. value = QPNP_DISABLE_PWM_MODE_ONLY_SUB_TYPE;
  895. mask = QPNP_PWM_MODE_ONLY_ENABLE_DISABLE_MASK_SUB_TYPE;
  896. } else {
  897. if (state == QPNP_PWM_ENABLE)
  898. value = qpnp_enable_pwm_mode(&pwm->pwm_config);
  899. else
  900. value = QPNP_DISABLE_PWM_MODE;
  901. mask = QPNP_EN_PWM_HIGH_MASK | QPNP_EN_PWM_LO_MASK |
  902. QPNP_EN_PWM_OUTPUT_MASK | QPNP_PWM_SRC_SELECT_MASK |
  903. QPNP_PWM_EN_RAMP_GEN_MASK;
  904. }
  905. rc = qpnp_lpg_save_and_write(value, mask,
  906. &pwm->chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL],
  907. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  908. QPNP_ENABLE_CONTROL), 1, chip);
  909. if (rc)
  910. goto out;
  911. /*
  912. * Due to LPG hardware bug, in the PWM mode, having enabled PWM,
  913. * We have to write PWM values one more time.
  914. */
  915. if (state == QPNP_PWM_ENABLE)
  916. return qpnp_lpg_save_pwm_value(pwm);
  917. out:
  918. return rc;
  919. }
  920. static int _pwm_config(struct pwm_device *pwm,
  921. enum time_level tm_lvl,
  922. int duty_value, int period_value)
  923. {
  924. struct qpnp_pwm_config *pwm_config;
  925. struct qpnp_lpg_chip *chip;
  926. struct pwm_period_config *period;
  927. int period_us, duty_us;
  928. int rc;
  929. chip = pwm->chip;
  930. pwm_config = &pwm->pwm_config;
  931. period = &pwm_config->period;
  932. if (tm_lvl == LVL_USEC) {
  933. period_us = period_value;
  934. duty_us = duty_value;
  935. } else {
  936. period_us = period_value / NSEC_PER_USEC;
  937. duty_us = duty_value / NSEC_PER_USEC;
  938. }
  939. if (pwm_config->pwm_period != period_us) {
  940. qpnp_lpg_calc_period(tm_lvl, period_value, pwm);
  941. qpnp_lpg_save_period(pwm);
  942. pwm_config->pwm_period = period_us;
  943. }
  944. pwm_config->pwm_duty = duty_us;
  945. qpnp_lpg_calc_pwm_value(pwm, period_value, duty_value);
  946. rc = qpnp_lpg_save_pwm_value(pwm);
  947. if (rc) {
  948. pr_err("Could not update PWM value for channel %d rc=%d\n",
  949. pwm_config->channel_id, rc);
  950. return rc;
  951. }
  952. rc = qpnp_lpg_configure_pwm(pwm);
  953. if (rc) {
  954. pr_err("Could not configure PWM clock for\n");
  955. pr_err("channel %d rc=%d\n", pwm_config->channel_id, rc);
  956. return rc;
  957. }
  958. rc = qpnp_configure_pwm_control(pwm);
  959. if (rc) {
  960. pr_err("Could not update PWM control for");
  961. pr_err("channel %d rc=%d\n", pwm_config->channel_id, rc);
  962. return rc;
  963. }
  964. pr_debug("duty/period=%u/%u %s: pwm_value=%d (of %d)\n",
  965. (unsigned)duty_us, (unsigned)period_us,
  966. (tm_lvl == LVL_USEC) ? "usec" : "nsec",
  967. pwm_config->pwm_value, 1 << period->pwm_size);
  968. return 0;
  969. }
  970. static int _pwm_lut_config(struct pwm_device *pwm, int period_us,
  971. int duty_pct[], struct lut_params lut_params)
  972. {
  973. struct qpnp_lpg_config *lpg_config;
  974. struct qpnp_lut_config *lut_config;
  975. struct pwm_period_config *period;
  976. struct qpnp_pwm_config *pwm_config;
  977. int start_idx = lut_params.start_idx;
  978. int len = lut_params.idx_len;
  979. int flags = lut_params.flags;
  980. int raw_lut, ramp_step_ms;
  981. int rc = 0;
  982. pwm_config = &pwm->pwm_config;
  983. lpg_config = &pwm->chip->lpg_config;
  984. lut_config = &lpg_config->lut_config;
  985. period = &pwm_config->period;
  986. if (pwm_config->pwm_period != period_us) {
  987. qpnp_lpg_calc_period(LVL_USEC, period_us, pwm);
  988. qpnp_lpg_save_period(pwm);
  989. pwm_config->pwm_period = period_us;
  990. }
  991. if (flags & PM_PWM_LUT_NO_TABLE)
  992. goto after_table_write;
  993. raw_lut = 0;
  994. if (flags & PM_PWM_LUT_USE_RAW_VALUE)
  995. raw_lut = 1;
  996. lut_config->list_len = len;
  997. lut_config->lo_index = start_idx + 1;
  998. lut_config->hi_index = start_idx + len;
  999. rc = qpnp_lpg_change_table(pwm, duty_pct, raw_lut);
  1000. if (rc) {
  1001. pr_err("qpnp_lpg_change_table: rc=%d\n", rc);
  1002. return -EINVAL;
  1003. }
  1004. after_table_write:
  1005. ramp_step_ms = lut_params.ramp_step_ms;
  1006. if (ramp_step_ms > PM_PWM_LUT_RAMP_STEP_TIME_MAX)
  1007. ramp_step_ms = PM_PWM_LUT_RAMP_STEP_TIME_MAX;
  1008. QPNP_SET_PAUSE_CNT(lut_config->lut_pause_lo_cnt,
  1009. lut_params.lut_pause_lo, ramp_step_ms);
  1010. if (lut_config->lut_pause_lo_cnt > PM_PWM_MAX_PAUSE_CNT)
  1011. lut_config->lut_pause_lo_cnt = PM_PWM_MAX_PAUSE_CNT;
  1012. QPNP_SET_PAUSE_CNT(lut_config->lut_pause_hi_cnt,
  1013. lut_params.lut_pause_hi, ramp_step_ms);
  1014. if (lut_config->lut_pause_hi_cnt > PM_PWM_MAX_PAUSE_CNT)
  1015. lut_config->lut_pause_hi_cnt = PM_PWM_MAX_PAUSE_CNT;
  1016. lut_config->ramp_step_ms = ramp_step_ms;
  1017. lut_config->ramp_direction = !!(flags & PM_PWM_LUT_RAMP_UP);
  1018. lut_config->pattern_repeat = !!(flags & PM_PWM_LUT_LOOP);
  1019. lut_config->ramp_toggle = !!(flags & PM_PWM_LUT_REVERSE);
  1020. lut_config->enable_pause_hi = !!(flags & PM_PWM_LUT_PAUSE_HI_EN);
  1021. lut_config->enable_pause_lo = !!(flags & PM_PWM_LUT_PAUSE_LO_EN);
  1022. rc = qpnp_lpg_change_lut(pwm);
  1023. return rc;
  1024. }
  1025. static int _pwm_enable(struct pwm_device *pwm)
  1026. {
  1027. int rc = 0;
  1028. struct qpnp_lpg_chip *chip;
  1029. unsigned long flags;
  1030. chip = pwm->chip;
  1031. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1032. if (QPNP_IS_PWM_CONFIG_SELECTED(
  1033. chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL]) ||
  1034. chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED) {
  1035. rc = qpnp_lpg_configure_pwm_state(pwm, QPNP_PWM_ENABLE);
  1036. } else if (!(chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED)) {
  1037. rc = qpnp_lpg_configure_lut_state(pwm, QPNP_LUT_ENABLE);
  1038. }
  1039. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1040. if (rc)
  1041. pr_err("Failed to enable PWM channel: %d\n",
  1042. pwm->pwm_config.channel_id);
  1043. return rc;
  1044. }
  1045. /* APIs */
  1046. /**
  1047. * pwm_request - request a PWM device
  1048. * @channel_id: PWM id or channel
  1049. * @lable: the label to identify the user
  1050. */
  1051. struct pwm_device *pwm_request(int pwm_id, const char *lable)
  1052. {
  1053. struct qpnp_lpg_chip *chip;
  1054. struct pwm_device *pwm;
  1055. unsigned long flags;
  1056. chip = radix_tree_lookup(&lpg_dev_tree, pwm_id);
  1057. if (!chip) {
  1058. pr_err("Could not find PWM Device for the\n");
  1059. pr_err("input pwm channel %d\n", pwm_id);
  1060. return ERR_PTR(-EINVAL);
  1061. }
  1062. spin_lock_irqsave(&chip->lpg_lock, flags);
  1063. pwm = &chip->pwm_dev;
  1064. if (pwm->pwm_config.in_use) {
  1065. pr_err("PWM device associated with the");
  1066. pr_err("input pwm id: %d is in use by %s",
  1067. pwm_id, pwm->pwm_config.lable);
  1068. pwm = ERR_PTR(-EBUSY);
  1069. } else {
  1070. pwm->pwm_config.in_use = 1;
  1071. pwm->pwm_config.lable = lable;
  1072. }
  1073. spin_unlock_irqrestore(&chip->lpg_lock, flags);
  1074. return pwm;
  1075. }
  1076. EXPORT_SYMBOL_GPL(pwm_request);
  1077. /**
  1078. * pwm_free - free a PWM device
  1079. * @pwm: the PWM device
  1080. */
  1081. void pwm_free(struct pwm_device *pwm)
  1082. {
  1083. struct qpnp_pwm_config *pwm_config;
  1084. unsigned long flags;
  1085. if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
  1086. pr_err("Invalid pwm handle or no pwm_chip\n");
  1087. return;
  1088. }
  1089. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1090. pwm_config = &pwm->pwm_config;
  1091. if (pwm_config->in_use) {
  1092. qpnp_lpg_configure_pwm_state(pwm, QPNP_PWM_DISABLE);
  1093. if (!(pwm->chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED))
  1094. qpnp_lpg_configure_lut_state(pwm, QPNP_LUT_DISABLE);
  1095. pwm_config->in_use = 0;
  1096. pwm_config->lable = NULL;
  1097. }
  1098. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1099. }
  1100. EXPORT_SYMBOL_GPL(pwm_free);
  1101. /**
  1102. * pwm_config - change a PWM device configuration
  1103. * @pwm: the PWM device
  1104. * @period_ns: period in nanoseconds
  1105. * @duty_ns: duty cycle in nanoseconds
  1106. */
  1107. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  1108. {
  1109. int rc;
  1110. unsigned long flags;
  1111. if (pwm == NULL || IS_ERR(pwm) || duty_ns > period_ns ||
  1112. (unsigned)period_ns < PM_PWM_PERIOD_MIN * NSEC_PER_USEC) {
  1113. pr_err("Invalid pwm handle or parameters\n");
  1114. return -EINVAL;
  1115. }
  1116. if (!pwm->pwm_config.in_use)
  1117. return -EINVAL;
  1118. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1119. rc = _pwm_config(pwm, LVL_NSEC, duty_ns, period_ns);
  1120. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1121. if (rc)
  1122. pr_err("Failed to configure PWM mode\n");
  1123. return rc;
  1124. }
  1125. EXPORT_SYMBOL_GPL(pwm_config);
  1126. /**
  1127. * pwm_config_us - change a PWM device configuration
  1128. * @pwm: the PWM device
  1129. * @period_us: period in microseconds
  1130. * @duty_us: duty cycle in microseconds
  1131. */
  1132. int pwm_config_us(struct pwm_device *pwm, int duty_us, int period_us)
  1133. {
  1134. int rc;
  1135. unsigned long flags;
  1136. if (pwm == NULL || IS_ERR(pwm) ||
  1137. duty_us > period_us ||
  1138. (unsigned)period_us > PM_PWM_PERIOD_MAX ||
  1139. (unsigned)period_us < PM_PWM_PERIOD_MIN) {
  1140. pr_err("Invalid pwm handle or parameters\n");
  1141. return -EINVAL;
  1142. }
  1143. if (!pwm->pwm_config.in_use)
  1144. return -EINVAL;
  1145. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1146. rc = _pwm_config(pwm, LVL_USEC, duty_us, period_us);
  1147. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1148. if (rc)
  1149. pr_err("Failed to configure PWM mode\n");
  1150. return rc;
  1151. }
  1152. EXPORT_SYMBOL_GPL(pwm_config_us);
  1153. /**
  1154. * pwm_enable - start a PWM output toggling
  1155. * @pwm: the PWM device
  1156. */
  1157. int pwm_enable(struct pwm_device *pwm)
  1158. {
  1159. struct qpnp_pwm_config *p_config;
  1160. if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
  1161. pr_err("Invalid pwm handle or no pwm_chip\n");
  1162. return -EINVAL;
  1163. }
  1164. p_config = &pwm->pwm_config;
  1165. if (!p_config->in_use) {
  1166. pr_err("channel_id: %d: stale handle?\n", p_config->channel_id);
  1167. return -EINVAL;
  1168. }
  1169. return _pwm_enable(pwm);
  1170. }
  1171. EXPORT_SYMBOL_GPL(pwm_enable);
  1172. /**
  1173. * pwm_disable - stop a PWM output toggling
  1174. * @pwm: the PWM device
  1175. */
  1176. void pwm_disable(struct pwm_device *pwm)
  1177. {
  1178. struct qpnp_pwm_config *pwm_config;
  1179. struct qpnp_lpg_chip *chip;
  1180. unsigned long flags;
  1181. int rc = 0;
  1182. if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
  1183. pr_err("Invalid pwm handle or no pwm_chip\n");
  1184. return;
  1185. }
  1186. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1187. chip = pwm->chip;
  1188. pwm_config = &pwm->pwm_config;
  1189. if (pwm_config->in_use) {
  1190. if (QPNP_IS_PWM_CONFIG_SELECTED(
  1191. chip->qpnp_lpg_registers[QPNP_ENABLE_CONTROL]) ||
  1192. chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED) {
  1193. rc = qpnp_lpg_configure_pwm_state(pwm,
  1194. QPNP_PWM_DISABLE);
  1195. } else if (!(chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED)) {
  1196. rc = qpnp_lpg_configure_lut_state(pwm,
  1197. QPNP_LUT_DISABLE);
  1198. }
  1199. }
  1200. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1201. if (rc)
  1202. pr_err("Failed to disable PWM channel: %d\n",
  1203. pwm_config->channel_id);
  1204. }
  1205. EXPORT_SYMBOL_GPL(pwm_disable);
  1206. /**
  1207. * pwm_change_mode - Change the PWM mode configuration
  1208. * @pwm: the PWM device
  1209. * @mode: Mode selection value
  1210. */
  1211. int pwm_change_mode(struct pwm_device *pwm, enum pm_pwm_mode mode)
  1212. {
  1213. int rc;
  1214. unsigned long flags;
  1215. if (pwm == NULL || IS_ERR(pwm) || pwm->chip == NULL) {
  1216. pr_err("Invalid pwm handle or no pwm_chip\n");
  1217. return -EINVAL;
  1218. }
  1219. if (mode < PM_PWM_MODE_PWM || mode > PM_PWM_MODE_LPG) {
  1220. pr_err("Invalid mode value\n");
  1221. return -EINVAL;
  1222. }
  1223. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1224. if (mode)
  1225. rc = qpnp_configure_lpg_control(pwm);
  1226. else
  1227. rc = qpnp_configure_pwm_control(pwm);
  1228. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1229. if (rc)
  1230. pr_err("Failed to change the mode\n");
  1231. return rc;
  1232. }
  1233. EXPORT_SYMBOL_GPL(pwm_change_mode);
  1234. /**
  1235. * pwm_config_period - change PWM period
  1236. *
  1237. * @pwm: the PWM device
  1238. * @pwm_p: period in struct qpnp_lpg_period
  1239. */
  1240. int pwm_config_period(struct pwm_device *pwm,
  1241. struct pwm_period_config *period)
  1242. {
  1243. struct qpnp_pwm_config *pwm_config;
  1244. struct qpnp_lpg_config *lpg_config;
  1245. struct qpnp_lpg_chip *chip;
  1246. unsigned long flags;
  1247. int rc = 0;
  1248. if (pwm == NULL || IS_ERR(pwm) || period == NULL)
  1249. return -EINVAL;
  1250. if (pwm->chip == NULL)
  1251. return -ENODEV;
  1252. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1253. chip = pwm->chip;
  1254. pwm_config = &pwm->pwm_config;
  1255. lpg_config = &chip->lpg_config;
  1256. if (!pwm_config->in_use) {
  1257. rc = -EINVAL;
  1258. goto out_unlock;
  1259. }
  1260. pwm_config->period.pwm_size = period->pwm_size;
  1261. pwm_config->period.clk = period->clk;
  1262. pwm_config->period.pre_div = period->pre_div;
  1263. pwm_config->period.pre_div_exp = period->pre_div_exp;
  1264. qpnp_lpg_save_period(pwm);
  1265. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  1266. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  1267. QPNP_LPG_PWM_SIZE_CLK),
  1268. &chip->qpnp_lpg_registers[QPNP_LPG_PWM_SIZE_CLK], 1);
  1269. if (rc) {
  1270. pr_err("Write failed: QPNP_LPG_PWM_SIZE_CLK register, rc: %d\n",
  1271. rc);
  1272. goto out_unlock;
  1273. }
  1274. rc = spmi_ext_register_writel(chip->spmi_dev->ctrl, chip->spmi_dev->sid,
  1275. SPMI_LPG_REG_ADDR(lpg_config->base_addr,
  1276. QPNP_LPG_PWM_FREQ_PREDIV_CLK),
  1277. &chip->qpnp_lpg_registers[QPNP_LPG_PWM_FREQ_PREDIV_CLK], 1);
  1278. if (rc) {
  1279. pr_err("Failed to write to QPNP_LPG_PWM_FREQ_PREDIV_CLK\n");
  1280. pr_err("register, rc = %d\n", rc);
  1281. }
  1282. out_unlock:
  1283. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1284. return rc;
  1285. }
  1286. EXPORT_SYMBOL(pwm_config_period);
  1287. /**
  1288. * pwm_config_pwm_value - change a PWM device configuration
  1289. * @pwm: the PWM device
  1290. * @pwm_value: the duty cycle in raw PWM value (< 2^pwm_size)
  1291. */
  1292. int pwm_config_pwm_value(struct pwm_device *pwm, int pwm_value)
  1293. {
  1294. struct qpnp_lpg_config *lpg_config;
  1295. struct qpnp_pwm_config *pwm_config;
  1296. unsigned long flags;
  1297. int rc = 0;
  1298. if (pwm == NULL || IS_ERR(pwm)) {
  1299. pr_err("Invalid parameter passed\n");
  1300. return -EINVAL;
  1301. }
  1302. if (pwm->chip == NULL) {
  1303. pr_err("Invalid device handle\n");
  1304. return -ENODEV;
  1305. }
  1306. lpg_config = &pwm->chip->lpg_config;
  1307. pwm_config = &pwm->pwm_config;
  1308. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1309. if (!pwm_config->in_use || !pwm_config->pwm_period) {
  1310. rc = -EINVAL;
  1311. pr_err("PWM channel isn't in use or period value missing\n");
  1312. goto out_unlock;
  1313. }
  1314. if (pwm_config->pwm_value == pwm_value)
  1315. goto out_unlock;
  1316. pwm_config->pwm_value = pwm_value;
  1317. rc = qpnp_lpg_save_pwm_value(pwm);
  1318. if (rc)
  1319. pr_err("Could not update PWM value for channel %d rc=%d\n",
  1320. pwm_config->channel_id, rc);
  1321. out_unlock:
  1322. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1323. return rc;
  1324. }
  1325. EXPORT_SYMBOL_GPL(pwm_config_pwm_value);
  1326. /**
  1327. * pwm_lut_config - change LPG LUT device configuration
  1328. * @pwm: the PWM device
  1329. * @period_us: period in micro second
  1330. * @duty_pct: array of duty cycles in percent, like 20, 50.
  1331. * @lut_params: Lookup table parameters
  1332. */
  1333. int pwm_lut_config(struct pwm_device *pwm, int period_us,
  1334. int duty_pct[], struct lut_params lut_params)
  1335. {
  1336. unsigned long flags;
  1337. int rc = 0;
  1338. if (pwm == NULL || IS_ERR(pwm) || !lut_params.idx_len) {
  1339. pr_err("Invalid pwm handle or idx_len=0\n");
  1340. return -EINVAL;
  1341. }
  1342. if (pwm->chip == NULL)
  1343. return -ENODEV;
  1344. if (pwm->chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED) {
  1345. pr_err("LUT mode isn't supported\n");
  1346. return -EINVAL;
  1347. }
  1348. if (!pwm->pwm_config.in_use) {
  1349. pr_err("channel_id: %d: stale handle?\n",
  1350. pwm->pwm_config.channel_id);
  1351. return -EINVAL;
  1352. }
  1353. if (duty_pct == NULL && !(lut_params.flags & PM_PWM_LUT_NO_TABLE)) {
  1354. pr_err("Invalid duty_pct with flag\n");
  1355. return -EINVAL;
  1356. }
  1357. if ((lut_params.start_idx + lut_params.idx_len) >
  1358. pwm->chip->lpg_config.lut_size) {
  1359. pr_err("Exceed LUT limit\n");
  1360. return -EINVAL;
  1361. }
  1362. if ((unsigned)period_us > PM_PWM_PERIOD_MAX ||
  1363. (unsigned)period_us < PM_PWM_PERIOD_MIN) {
  1364. pr_err("Period out of range\n");
  1365. return -EINVAL;
  1366. }
  1367. spin_lock_irqsave(&pwm->chip->lpg_lock, flags);
  1368. rc = _pwm_lut_config(pwm, period_us, duty_pct, lut_params);
  1369. spin_unlock_irqrestore(&pwm->chip->lpg_lock, flags);
  1370. if (rc)
  1371. pr_err("Failed to configure LUT\n");
  1372. return rc;
  1373. }
  1374. EXPORT_SYMBOL_GPL(pwm_lut_config);
  1375. static int qpnp_parse_pwm_dt_config(struct device_node *of_pwm_node,
  1376. struct device_node *of_parent, struct qpnp_lpg_chip *chip)
  1377. {
  1378. int rc, period;
  1379. struct pwm_device *pwm_dev = &chip->pwm_dev;
  1380. rc = of_property_read_u32(of_parent, "qcom,period", (u32 *)&period);
  1381. if (rc) {
  1382. pr_err("node is missing PWM Period prop");
  1383. return rc;
  1384. }
  1385. rc = of_property_read_u32(of_pwm_node, "qcom,duty",
  1386. &pwm_dev->pwm_config.pwm_duty);
  1387. if (rc) {
  1388. pr_err("node is missing PWM Duty prop");
  1389. return rc;
  1390. }
  1391. rc = _pwm_config(pwm_dev, LVL_USEC,
  1392. pwm_dev->pwm_config.pwm_duty, period);
  1393. return rc;
  1394. }
  1395. #define qpnp_check_optional_dt_bindings(func) \
  1396. do { \
  1397. rc = func; \
  1398. if (rc && rc != -EINVAL) \
  1399. goto out; \
  1400. rc = 0; \
  1401. } while (0);
  1402. static int qpnp_parse_lpg_dt_config(struct device_node *of_lpg_node,
  1403. struct device_node *of_parent, struct qpnp_lpg_chip *chip)
  1404. {
  1405. int rc, period, list_size, start_idx, *duty_pct_list;
  1406. struct pwm_device *pwm_dev = &chip->pwm_dev;
  1407. struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
  1408. struct qpnp_lut_config *lut_config = &lpg_config->lut_config;
  1409. struct lut_params lut_params;
  1410. rc = of_property_read_u32(of_parent, "qcom,period", &period);
  1411. if (rc) {
  1412. pr_err("node is missing PWM Period prop");
  1413. return rc;
  1414. }
  1415. if (!of_get_property(of_lpg_node, "qcom,duty-percents", &list_size)) {
  1416. pr_err("node is missing duty-pct list");
  1417. return rc;
  1418. }
  1419. rc = of_property_read_u32(of_lpg_node, "cell-index", &start_idx);
  1420. if (rc) {
  1421. pr_err("Missing start index");
  1422. return rc;
  1423. }
  1424. list_size /= sizeof(u32);
  1425. if (list_size + start_idx > lpg_config->lut_size) {
  1426. pr_err("duty pct list size overflows\n");
  1427. return -EINVAL;
  1428. }
  1429. duty_pct_list = kzalloc(sizeof(u32) * list_size, GFP_KERNEL);
  1430. if (!duty_pct_list) {
  1431. pr_err("kzalloc failed on duty_pct_list\n");
  1432. return -ENOMEM;
  1433. }
  1434. rc = of_property_read_u32_array(of_lpg_node, "qcom,duty-percents",
  1435. duty_pct_list, list_size);
  1436. if (rc) {
  1437. pr_err("invalid or missing property:\n");
  1438. pr_err("qcom,duty-pcts-list\n");
  1439. kfree(duty_pct_list);
  1440. return rc;
  1441. }
  1442. /* Read optional properties */
  1443. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1444. "qcom,ramp-step-duration", &lut_config->ramp_step_ms));
  1445. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1446. "qcom,lpg-lut-pause-hi", &lut_config->lut_pause_hi_cnt));
  1447. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1448. "qcom,lpg-lut-pause-lo", &lut_config->lut_pause_lo_cnt));
  1449. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1450. "qcom,lpg-lut-ramp-direction",
  1451. (u32 *)&lut_config->ramp_direction));
  1452. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1453. "qcom,lpg-lut-pattern-repeat",
  1454. (u32 *)&lut_config->pattern_repeat));
  1455. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1456. "qcom,lpg-lut-ramp-toggle",
  1457. (u32 *)&lut_config->ramp_toggle));
  1458. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1459. "qcom,lpg-lut-enable-pause-hi",
  1460. (u32 *)&lut_config->enable_pause_hi));
  1461. qpnp_check_optional_dt_bindings(of_property_read_u32(of_lpg_node,
  1462. "qcom,lpg-lut-enable-pause-lo",
  1463. (u32 *)&lut_config->enable_pause_lo));
  1464. qpnp_set_lut_params(&lut_params, lut_config, start_idx, list_size);
  1465. _pwm_lut_config(pwm_dev, period, duty_pct_list, lut_params);
  1466. out:
  1467. kfree(duty_pct_list);
  1468. return rc;
  1469. }
  1470. /* Fill in lpg device elements based on values found in device tree. */
  1471. static int qpnp_parse_dt_config(struct spmi_device *spmi,
  1472. struct qpnp_lpg_chip *chip)
  1473. {
  1474. int rc, enable, lut_entry_size;
  1475. const char *lable;
  1476. struct resource *res;
  1477. struct device_node *node;
  1478. int found_pwm_subnode = 0;
  1479. int found_lpg_subnode = 0;
  1480. struct device_node *of_node = spmi->dev.of_node;
  1481. struct pwm_device *pwm_dev = &chip->pwm_dev;
  1482. struct qpnp_lpg_config *lpg_config = &chip->lpg_config;
  1483. struct qpnp_lut_config *lut_config = &lpg_config->lut_config;
  1484. int force_pwm_size = 0;
  1485. rc = of_property_read_u32(of_node, "qcom,channel-id",
  1486. &pwm_dev->pwm_config.channel_id);
  1487. if (rc) {
  1488. dev_err(&spmi->dev, "%s: node is missing LPG channel id\n",
  1489. __func__);
  1490. goto out;
  1491. }
  1492. /*
  1493. * For cetrain LPG channels PWM size can be forced. So that
  1494. * for every requested pwm period closest pwm frequency is
  1495. * selected in qpnp_lpg_calc_period() for the forced pwm size.
  1496. */
  1497. rc = of_property_read_u32(of_node, "qcom,force-pwm-size",
  1498. &force_pwm_size);
  1499. if (qpnp_check_gpled_lpg_channel(pwm_dev->pwm_config.channel_id)) {
  1500. if (!(force_pwm_size == QPNP_PWM_SIZE_7_BIT ||
  1501. force_pwm_size == QPNP_PWM_SIZE_8_BIT))
  1502. force_pwm_size = 0;
  1503. } else if (chip->sub_type == QPNP_PWM_MODE_ONLY_SUB_TYPE) {
  1504. if (!(force_pwm_size == QPNP_PWM_SIZE_6_BIT ||
  1505. force_pwm_size == QPNP_PWM_SIZE_9_BIT))
  1506. force_pwm_size = 0;
  1507. } else if (!(force_pwm_size == QPNP_PWM_SIZE_6_BIT ||
  1508. force_pwm_size == QPNP_PWM_SIZE_7_BIT ||
  1509. force_pwm_size == QPNP_PWM_SIZE_9_BIT))
  1510. force_pwm_size = 0;
  1511. pwm_dev->pwm_config.force_pwm_size = force_pwm_size;
  1512. res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM,
  1513. QPNP_LPG_CHANNEL_BASE);
  1514. if (!res) {
  1515. dev_err(&spmi->dev, "%s: node is missing base address\n",
  1516. __func__);
  1517. return -EINVAL;
  1518. }
  1519. lpg_config->base_addr = res->start;
  1520. res = spmi_get_resource_byname(spmi, NULL, IORESOURCE_MEM,
  1521. QPNP_LPG_LUT_BASE);
  1522. if (!res) {
  1523. chip->flags |= QPNP_PWM_LUT_NOT_SUPPORTED;
  1524. } else {
  1525. lpg_config->lut_base_addr = res->start;
  1526. /* Each entry of LUT is of 2 bytes for generic LUT and of 1 byte
  1527. * for KPDBL/GLED LUT.
  1528. */
  1529. lpg_config->lut_size = resource_size(res) >> 1;
  1530. lut_entry_size = sizeof(u16);
  1531. if (qpnp_check_gpled_lpg_channel(
  1532. pwm_dev->pwm_config.channel_id)) {
  1533. lpg_config->lut_size = resource_size(res);
  1534. lut_entry_size = sizeof(u8);
  1535. }
  1536. lut_config->duty_pct_list = kzalloc(lpg_config->lut_size *
  1537. lut_entry_size, GFP_KERNEL);
  1538. if (!lut_config->duty_pct_list) {
  1539. pr_err("can not allocate duty pct list\n");
  1540. return -ENOMEM;
  1541. }
  1542. }
  1543. for_each_child_of_node(of_node, node) {
  1544. rc = of_property_read_string(node, "label", &lable);
  1545. if (rc) {
  1546. dev_err(&spmi->dev, "%s: Missing lable property\n",
  1547. __func__);
  1548. goto out;
  1549. }
  1550. if (!strncmp(lable, "pwm", 3)) {
  1551. rc = qpnp_parse_pwm_dt_config(node, of_node, chip);
  1552. if (rc)
  1553. goto out;
  1554. found_pwm_subnode = 1;
  1555. } else if (!strncmp(lable, "lpg", 3) &&
  1556. !(chip->flags & QPNP_PWM_LUT_NOT_SUPPORTED)) {
  1557. qpnp_parse_lpg_dt_config(node, of_node, chip);
  1558. if (rc)
  1559. goto out;
  1560. found_lpg_subnode = 1;
  1561. } else {
  1562. dev_err(&spmi->dev, "%s: Invalid value for lable prop",
  1563. __func__);
  1564. }
  1565. }
  1566. rc = of_property_read_u32(of_node, "qcom,mode-select", &enable);
  1567. if (rc)
  1568. goto read_opt_props;
  1569. if ((enable == PM_PWM_MODE_PWM && found_pwm_subnode == 0) ||
  1570. (enable == PM_PWM_MODE_LPG && found_lpg_subnode == 0)) {
  1571. dev_err(&spmi->dev, "%s: Invalid mode select\n", __func__);
  1572. rc = -EINVAL;
  1573. goto out;
  1574. }
  1575. pwm_change_mode(pwm_dev, enable);
  1576. _pwm_enable(pwm_dev);
  1577. read_opt_props:
  1578. /* Initialize optional config parameters from DT if provided */
  1579. of_property_read_string(node, "qcom,channel-owner",
  1580. &pwm_dev->pwm_config.lable);
  1581. return 0;
  1582. out:
  1583. kfree(lut_config->duty_pct_list);
  1584. return rc;
  1585. }
  1586. static int __devinit qpnp_pwm_probe(struct spmi_device *spmi)
  1587. {
  1588. struct qpnp_lpg_chip *chip;
  1589. int rc, id;
  1590. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  1591. if (chip == NULL) {
  1592. pr_err("kzalloc() failed.\n");
  1593. return -ENOMEM;
  1594. }
  1595. spin_lock_init(&chip->lpg_lock);
  1596. chip->spmi_dev = spmi;
  1597. chip->pwm_dev.chip = chip;
  1598. dev_set_drvdata(&spmi->dev, chip);
  1599. rc = qpnp_parse_dt_config(spmi, chip);
  1600. if (rc)
  1601. goto failed_config;
  1602. id = chip->pwm_dev.pwm_config.channel_id;
  1603. spmi_ext_register_readl(chip->spmi_dev->ctrl,
  1604. chip->spmi_dev->sid,
  1605. chip->lpg_config.base_addr + SPMI_LPG_REVISION2_OFFSET,
  1606. (u8 *) &chip->revision, 1);
  1607. if (chip->revision < QPNP_LPG_REVISION_0 ||
  1608. chip->revision > QPNP_LPG_REVISION_1) {
  1609. pr_err("Unknown LPG revision detected, rev:%d\n",
  1610. chip->revision);
  1611. rc = -EINVAL;
  1612. goto failed_insert;
  1613. }
  1614. spmi_ext_register_readl(chip->spmi_dev->ctrl,
  1615. chip->spmi_dev->sid,
  1616. chip->lpg_config.base_addr + SPMI_LPG_SUB_TYPE_OFFSET,
  1617. &chip->sub_type, 1);
  1618. rc = radix_tree_insert(&lpg_dev_tree, id, chip);
  1619. if (rc) {
  1620. dev_err(&spmi->dev, "%s: Failed to register LPG Channel %d\n",
  1621. __func__, id);
  1622. goto failed_insert;
  1623. }
  1624. return 0;
  1625. failed_insert:
  1626. kfree(chip->lpg_config.lut_config.duty_pct_list);
  1627. failed_config:
  1628. dev_set_drvdata(&spmi->dev, NULL);
  1629. kfree(chip);
  1630. return rc;
  1631. }
  1632. static int __devexit qpnp_pwm_remove(struct spmi_device *spmi)
  1633. {
  1634. struct qpnp_lpg_chip *chip;
  1635. struct qpnp_lpg_config *lpg_config;
  1636. chip = dev_get_drvdata(&spmi->dev);
  1637. dev_set_drvdata(&spmi->dev, NULL);
  1638. if (chip) {
  1639. lpg_config = &chip->lpg_config;
  1640. kfree(lpg_config->lut_config.duty_pct_list);
  1641. kfree(chip);
  1642. }
  1643. return 0;
  1644. }
  1645. static struct of_device_id spmi_match_table[] = {
  1646. { .compatible = QPNP_LPG_DRIVER_NAME, },
  1647. {}
  1648. };
  1649. static const struct spmi_device_id qpnp_lpg_id[] = {
  1650. { QPNP_LPG_DRIVER_NAME, 0 },
  1651. { }
  1652. };
  1653. MODULE_DEVICE_TABLE(spmi, qpnp_lpg_id);
  1654. static struct spmi_driver qpnp_lpg_driver = {
  1655. .driver = {
  1656. .name = QPNP_LPG_DRIVER_NAME,
  1657. .of_match_table = spmi_match_table,
  1658. .owner = THIS_MODULE,
  1659. },
  1660. .probe = qpnp_pwm_probe,
  1661. .remove = __devexit_p(qpnp_pwm_remove),
  1662. .id_table = qpnp_lpg_id,
  1663. };
  1664. /**
  1665. * qpnp_lpg_init() - register spmi driver for qpnp-lpg
  1666. */
  1667. int __init qpnp_lpg_init(void)
  1668. {
  1669. return spmi_driver_register(&qpnp_lpg_driver);
  1670. }
  1671. static void __exit qpnp_lpg_exit(void)
  1672. {
  1673. spmi_driver_unregister(&qpnp_lpg_driver);
  1674. }
  1675. MODULE_DESCRIPTION("QPNP PMIC LPG driver");
  1676. MODULE_LICENSE("GPL v2");
  1677. MODULE_ALIAS("platform:" QPNP_LPG_DRIVER_NAME);
  1678. subsys_initcall(qpnp_lpg_init);
  1679. module_exit(qpnp_lpg_exit);