core.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic pwmlib implementation
  4. *
  5. * Copyright (C) 2011 Sascha Hauer <s.hauer@pengutronix.de>
  6. * Copyright (C) 2011-2012 Avionic Design GmbH
  7. */
  8. #define DEFAULT_SYMBOL_NAMESPACE "PWM"
  9. #include <linux/acpi.h>
  10. #include <linux/module.h>
  11. #include <linux/idr.h>
  12. #include <linux/of.h>
  13. #include <linux/pwm.h>
  14. #include <linux/list.h>
  15. #include <linux/mutex.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/device.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/seq_file.h>
  21. #include <dt-bindings/pwm/pwm.h>
  22. #include <uapi/linux/pwm.h>
  23. #define CREATE_TRACE_POINTS
  24. #include <trace/events/pwm.h>
  25. #define PWM_MINOR_COUNT 256
  26. /* protects access to pwm_chips */
  27. static DEFINE_MUTEX(pwm_lock);
  28. static DEFINE_IDR(pwm_chips);
  29. static void pwmchip_lock(struct pwm_chip *chip)
  30. {
  31. if (chip->atomic)
  32. spin_lock(&chip->atomic_lock);
  33. else
  34. mutex_lock(&chip->nonatomic_lock);
  35. }
  36. static void pwmchip_unlock(struct pwm_chip *chip)
  37. {
  38. if (chip->atomic)
  39. spin_unlock(&chip->atomic_lock);
  40. else
  41. mutex_unlock(&chip->nonatomic_lock);
  42. }
  43. DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T))
  44. static bool pwm_wf_valid(const struct pwm_waveform *wf)
  45. {
  46. /*
  47. * For now restrict waveforms to period_length_ns <= S64_MAX to provide
  48. * some space for future extensions. One possibility is to simplify
  49. * representing waveforms with inverted polarity using negative values
  50. * somehow.
  51. */
  52. if (wf->period_length_ns > S64_MAX)
  53. return false;
  54. if (wf->duty_length_ns > wf->period_length_ns)
  55. return false;
  56. /*
  57. * .duty_offset_ns is supposed to be smaller than .period_length_ns, apart
  58. * from the corner case .duty_offset_ns == 0 && .period_length_ns == 0.
  59. */
  60. if (wf->duty_offset_ns && wf->duty_offset_ns >= wf->period_length_ns)
  61. return false;
  62. return true;
  63. }
  64. static void pwm_wf2state(const struct pwm_waveform *wf, struct pwm_state *state)
  65. {
  66. if (wf->period_length_ns) {
  67. if (wf->duty_length_ns + wf->duty_offset_ns < wf->period_length_ns)
  68. *state = (struct pwm_state){
  69. .enabled = true,
  70. .polarity = PWM_POLARITY_NORMAL,
  71. .period = wf->period_length_ns,
  72. .duty_cycle = wf->duty_length_ns,
  73. };
  74. else
  75. *state = (struct pwm_state){
  76. .enabled = true,
  77. .polarity = PWM_POLARITY_INVERSED,
  78. .period = wf->period_length_ns,
  79. .duty_cycle = wf->period_length_ns - wf->duty_length_ns,
  80. };
  81. } else {
  82. *state = (struct pwm_state){
  83. .enabled = false,
  84. };
  85. }
  86. }
  87. static void pwm_state2wf(const struct pwm_state *state, struct pwm_waveform *wf)
  88. {
  89. if (state->enabled) {
  90. if (state->polarity == PWM_POLARITY_NORMAL)
  91. *wf = (struct pwm_waveform){
  92. .period_length_ns = state->period,
  93. .duty_length_ns = state->duty_cycle,
  94. .duty_offset_ns = 0,
  95. };
  96. else
  97. *wf = (struct pwm_waveform){
  98. .period_length_ns = state->period,
  99. .duty_length_ns = state->period - state->duty_cycle,
  100. .duty_offset_ns = state->duty_cycle,
  101. };
  102. } else {
  103. *wf = (struct pwm_waveform){
  104. .period_length_ns = 0,
  105. };
  106. }
  107. }
  108. static int pwmwfcmp(const struct pwm_waveform *a, const struct pwm_waveform *b)
  109. {
  110. if (a->period_length_ns > b->period_length_ns)
  111. return 1;
  112. if (a->period_length_ns < b->period_length_ns)
  113. return -1;
  114. if (a->duty_length_ns > b->duty_length_ns)
  115. return 1;
  116. if (a->duty_length_ns < b->duty_length_ns)
  117. return -1;
  118. if (a->duty_offset_ns > b->duty_offset_ns)
  119. return 1;
  120. if (a->duty_offset_ns < b->duty_offset_ns)
  121. return -1;
  122. return 0;
  123. }
  124. static bool pwm_check_rounding(const struct pwm_waveform *wf,
  125. const struct pwm_waveform *wf_rounded)
  126. {
  127. if (!wf->period_length_ns)
  128. return true;
  129. if (wf->period_length_ns < wf_rounded->period_length_ns)
  130. return false;
  131. if (wf->duty_length_ns < wf_rounded->duty_length_ns)
  132. return false;
  133. if (wf->duty_offset_ns < wf_rounded->duty_offset_ns)
  134. return false;
  135. return true;
  136. }
  137. static int __pwm_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm,
  138. const struct pwm_waveform *wf, void *wfhw)
  139. {
  140. const struct pwm_ops *ops = chip->ops;
  141. int ret;
  142. ret = ops->round_waveform_tohw(chip, pwm, wf, wfhw);
  143. trace_pwm_round_waveform_tohw(pwm, wf, wfhw, ret);
  144. return ret;
  145. }
  146. static int __pwm_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm,
  147. const void *wfhw, struct pwm_waveform *wf)
  148. {
  149. const struct pwm_ops *ops = chip->ops;
  150. int ret;
  151. ret = ops->round_waveform_fromhw(chip, pwm, wfhw, wf);
  152. trace_pwm_round_waveform_fromhw(pwm, wfhw, wf, ret);
  153. return ret;
  154. }
  155. static int __pwm_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw)
  156. {
  157. const struct pwm_ops *ops = chip->ops;
  158. int ret;
  159. ret = ops->read_waveform(chip, pwm, wfhw);
  160. trace_pwm_read_waveform(pwm, wfhw, ret);
  161. return ret;
  162. }
  163. static int __pwm_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw)
  164. {
  165. const struct pwm_ops *ops = chip->ops;
  166. int ret;
  167. ret = ops->write_waveform(chip, pwm, wfhw);
  168. trace_pwm_write_waveform(pwm, wfhw, ret);
  169. return ret;
  170. }
  171. /**
  172. * pwm_round_waveform_might_sleep - Query hardware capabilities
  173. * Cannot be used in atomic context.
  174. * @pwm: PWM device
  175. * @wf: waveform to round and output parameter
  176. *
  177. * Typically a given waveform cannot be implemented exactly by hardware, e.g.
  178. * because hardware only supports coarse period resolution or no duty_offset.
  179. * This function returns the actually implemented waveform if you pass @wf to
  180. * pwm_set_waveform_might_sleep() now.
  181. *
  182. * Note however that the world doesn't stop turning when you call it, so when
  183. * doing::
  184. *
  185. * pwm_round_waveform_might_sleep(mypwm, &wf);
  186. * pwm_set_waveform_might_sleep(mypwm, &wf, true);
  187. *
  188. * the latter might fail, e.g. because an input clock changed its rate between
  189. * these two calls and the waveform determined by
  190. * pwm_round_waveform_might_sleep() cannot be implemented any more.
  191. *
  192. * Usually all values passed in @wf are rounded down to the nearest possible
  193. * value (in the order period_length_ns, duty_length_ns and then
  194. * duty_offset_ns). Only if this isn't possible, a value might grow. See the
  195. * documentation for pwm_set_waveform_might_sleep() for a more formal
  196. * description.
  197. *
  198. * Returns: 0 on success, 1 if at least one value had to be rounded up or a
  199. * negative errno.
  200. * Context: May sleep.
  201. */
  202. int pwm_round_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf)
  203. {
  204. struct pwm_chip *chip = pwm->chip;
  205. const struct pwm_ops *ops = chip->ops;
  206. struct pwm_waveform wf_req = *wf;
  207. char wfhw[PWM_WFHWSIZE];
  208. int ret_tohw, ret_fromhw;
  209. BUG_ON(PWM_WFHWSIZE < ops->sizeof_wfhw);
  210. if (!pwmchip_supports_waveform(chip))
  211. return -EOPNOTSUPP;
  212. if (!pwm_wf_valid(wf))
  213. return -EINVAL;
  214. guard(pwmchip)(chip);
  215. if (!chip->operational)
  216. return -ENODEV;
  217. ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, wfhw);
  218. if (ret_tohw < 0)
  219. return ret_tohw;
  220. if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_tohw > 1)
  221. dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_tohw: requested %llu/%llu [+%llu], return value %d\n",
  222. wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw);
  223. ret_fromhw = __pwm_round_waveform_fromhw(chip, pwm, wfhw, wf);
  224. if (ret_fromhw < 0)
  225. return ret_fromhw;
  226. if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_fromhw > 0)
  227. dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu [+%llu], return value %d\n",
  228. wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_fromhw);
  229. if (IS_ENABLED(CONFIG_PWM_DEBUG) &&
  230. (ret_tohw == 0) != pwm_check_rounding(&wf_req, wf))
  231. dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu], ret: %d\n",
  232. wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns,
  233. wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, ret_tohw);
  234. return ret_tohw;
  235. }
  236. EXPORT_SYMBOL_GPL(pwm_round_waveform_might_sleep);
  237. /**
  238. * pwm_get_waveform_might_sleep - Query hardware about current configuration
  239. * Cannot be used in atomic context.
  240. * @pwm: PWM device
  241. * @wf: output parameter
  242. *
  243. * Stores the current configuration of the PWM in @wf. Note this is the
  244. * equivalent of pwm_get_state_hw() (and not pwm_get_state()) for pwm_waveform.
  245. *
  246. * Returns: 0 on success or a negative errno
  247. * Context: May sleep.
  248. */
  249. int pwm_get_waveform_might_sleep(struct pwm_device *pwm, struct pwm_waveform *wf)
  250. {
  251. struct pwm_chip *chip = pwm->chip;
  252. const struct pwm_ops *ops = chip->ops;
  253. char wfhw[PWM_WFHWSIZE];
  254. int err;
  255. BUG_ON(PWM_WFHWSIZE < ops->sizeof_wfhw);
  256. if (!pwmchip_supports_waveform(chip) || !ops->read_waveform)
  257. return -EOPNOTSUPP;
  258. guard(pwmchip)(chip);
  259. if (!chip->operational)
  260. return -ENODEV;
  261. err = __pwm_read_waveform(chip, pwm, &wfhw);
  262. if (err)
  263. return err;
  264. return __pwm_round_waveform_fromhw(chip, pwm, &wfhw, wf);
  265. }
  266. EXPORT_SYMBOL_GPL(pwm_get_waveform_might_sleep);
  267. /* Called with the pwmchip lock held */
  268. static int __pwm_set_waveform(struct pwm_device *pwm,
  269. const struct pwm_waveform *wf,
  270. bool exact)
  271. {
  272. struct pwm_chip *chip = pwm->chip;
  273. const struct pwm_ops *ops = chip->ops;
  274. char wfhw[PWM_WFHWSIZE];
  275. struct pwm_waveform wf_rounded;
  276. int err, ret_tohw;
  277. BUG_ON(PWM_WFHWSIZE < ops->sizeof_wfhw);
  278. if (!pwmchip_supports_waveform(chip))
  279. return -EOPNOTSUPP;
  280. if (!pwm_wf_valid(wf))
  281. return -EINVAL;
  282. ret_tohw = __pwm_round_waveform_tohw(chip, pwm, wf, &wfhw);
  283. if (ret_tohw < 0)
  284. return ret_tohw;
  285. if ((IS_ENABLED(CONFIG_PWM_DEBUG) || exact) && wf->period_length_ns) {
  286. err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
  287. if (err)
  288. return err;
  289. if (IS_ENABLED(CONFIG_PWM_DEBUG) && (ret_tohw == 0) != pwm_check_rounding(wf, &wf_rounded))
  290. dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu], ret: %d\n",
  291. wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
  292. wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns, ret_tohw);
  293. if (exact && pwmwfcmp(wf, &wf_rounded)) {
  294. dev_dbg(&chip->dev, "Requested no rounding, but %llu/%llu [+%llu] -> %llu/%llu [+%llu]\n",
  295. wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
  296. wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
  297. return 1;
  298. }
  299. }
  300. err = __pwm_write_waveform(chip, pwm, &wfhw);
  301. if (err)
  302. return err;
  303. /* update .state */
  304. pwm_wf2state(wf, &pwm->state);
  305. if (IS_ENABLED(CONFIG_PWM_DEBUG) && ops->read_waveform && wf->period_length_ns) {
  306. struct pwm_waveform wf_set;
  307. err = __pwm_read_waveform(chip, pwm, &wfhw);
  308. if (err)
  309. /* maybe ignore? */
  310. return err;
  311. err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_set);
  312. if (err)
  313. /* maybe ignore? */
  314. return err;
  315. if (pwmwfcmp(&wf_set, &wf_rounded) != 0)
  316. dev_err(&chip->dev,
  317. "Unexpected setting: requested %llu/%llu [+%llu], expected %llu/%llu [+%llu], set %llu/%llu [+%llu]\n",
  318. wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns,
  319. wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns,
  320. wf_set.duty_length_ns, wf_set.period_length_ns, wf_set.duty_offset_ns);
  321. }
  322. return ret_tohw;
  323. }
  324. /**
  325. * pwm_set_waveform_might_sleep - Apply a new waveform
  326. * Cannot be used in atomic context.
  327. * @pwm: PWM device
  328. * @wf: The waveform to apply
  329. * @exact: If true no rounding is allowed
  330. *
  331. * Typically a requested waveform cannot be implemented exactly, e.g. because
  332. * you requested .period_length_ns = 100 ns, but the hardware can only set
  333. * periods that are a multiple of 8.5 ns. With that hardware passing @exact =
  334. * true results in pwm_set_waveform_might_sleep() failing and returning -EDOM.
  335. * If @exact = false you get a period of 93.5 ns (i.e. the biggest period not
  336. * bigger than the requested value).
  337. * Note that even with @exact = true, some rounding by less than 1 ns is
  338. * possible/needed. In the above example requesting .period_length_ns = 94 and
  339. * @exact = true, you get the hardware configured with period = 93.5 ns.
  340. *
  341. * Let C be the set of possible hardware configurations for a given PWM device,
  342. * consisting of tuples (p, d, o) where p is the period length, d is the duty
  343. * length and o the duty offset.
  344. *
  345. * The following algorithm is implemented to pick the hardware setting
  346. * (p, d, o) ∈ C for a given request (p', d', o') with @exact = false::
  347. *
  348. * p = max( { ṗ | ∃ ḋ, ȯ : (ṗ, ḋ, ȯ) ∈ C ∧ ṗ ≤ p' } ∪ { min({ ṗ | ∃ ḋ, ȯ : (ṗ, ḋ, ȯ) ∈ C }) })
  349. * d = max( { ḋ | ∃ ȯ : (p, ḋ, ȯ) ∈ C ∧ ḋ ≤ d' } ∪ { min({ ḋ | ∃ ȯ : (p, ḋ, ȯ) ∈ C }) })
  350. * o = max( { ȯ | (p, d, ȯ) ∈ C ∧ ȯ ≤ o' } ∪ { min({ ȯ | (p, d, ȯ) ∈ C }) })
  351. *
  352. * In words: The chosen period length is the maximal possible period length not
  353. * bigger than the requested period length and if that doesn't exist, the
  354. * minimal period length. The chosen duty length is the maximal possible duty
  355. * length that is compatible with the chosen period length and isn't bigger than
  356. * the requested duty length. Again if such a value doesn't exist, the minimal
  357. * duty length compatible with the chosen period is picked. After that the duty
  358. * offset compatible with the chosen period and duty length is chosen in the
  359. * same way.
  360. *
  361. * Returns: 0 on success, -EDOM if setting failed due to the exact waveform not
  362. * being possible (if @exact), or a different negative errno on failure.
  363. * Context: May sleep.
  364. */
  365. int pwm_set_waveform_might_sleep(struct pwm_device *pwm,
  366. const struct pwm_waveform *wf, bool exact)
  367. {
  368. struct pwm_chip *chip = pwm->chip;
  369. int err;
  370. might_sleep();
  371. guard(pwmchip)(chip);
  372. if (!chip->operational)
  373. return -ENODEV;
  374. if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
  375. /*
  376. * Catch any drivers that have been marked as atomic but
  377. * that will sleep anyway.
  378. */
  379. non_block_start();
  380. err = __pwm_set_waveform(pwm, wf, exact);
  381. non_block_end();
  382. } else {
  383. err = __pwm_set_waveform(pwm, wf, exact);
  384. }
  385. /*
  386. * map err == 1 to -EDOM for exact requests and 0 for !exact ones. Also
  387. * make sure that -EDOM is only returned in exactly that case. Note that
  388. * __pwm_set_waveform() should never return -EDOM which justifies the
  389. * unlikely().
  390. */
  391. if (unlikely(err == -EDOM))
  392. err = -EINVAL;
  393. else if (exact && err == 1)
  394. err = -EDOM;
  395. else if (err == 1)
  396. err = 0;
  397. return err;
  398. }
  399. EXPORT_SYMBOL_GPL(pwm_set_waveform_might_sleep);
  400. static void pwm_apply_debug(struct pwm_device *pwm,
  401. const struct pwm_state *state)
  402. {
  403. struct pwm_state *last = &pwm->last;
  404. struct pwm_chip *chip = pwm->chip;
  405. struct pwm_state s1 = { 0 }, s2 = { 0 };
  406. int err;
  407. if (!IS_ENABLED(CONFIG_PWM_DEBUG))
  408. return;
  409. /* No reasonable diagnosis possible without .get_state() */
  410. if (!chip->ops->get_state)
  411. return;
  412. /*
  413. * If a disabled PWM was requested the result is unspecified, so nothing
  414. * to check.
  415. */
  416. if (!state->enabled)
  417. return;
  418. /*
  419. * *state was just applied. Read out the hardware state and do some
  420. * checks.
  421. */
  422. err = chip->ops->get_state(chip, pwm, &s1);
  423. trace_pwm_get(pwm, &s1, err);
  424. if (err)
  425. /* If that failed there isn't much to debug */
  426. return;
  427. /*
  428. * If the PWM was disabled that's maybe strange but there is nothing
  429. * that can be sensibly checked then. So return early.
  430. */
  431. if (!s1.enabled)
  432. return;
  433. /*
  434. * The lowlevel driver either ignored .polarity (which is a bug) or as
  435. * best effort inverted .polarity and fixed .duty_cycle respectively.
  436. * Undo this inversion and fixup for further tests.
  437. */
  438. if (s1.polarity != state->polarity) {
  439. s2.polarity = state->polarity;
  440. s2.duty_cycle = s1.period - s1.duty_cycle;
  441. s2.period = s1.period;
  442. s2.enabled = true;
  443. } else {
  444. s2 = s1;
  445. }
  446. if (s2.polarity != state->polarity &&
  447. s2.duty_cycle < s2.period)
  448. dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
  449. if (last->polarity == state->polarity &&
  450. last->period > s2.period &&
  451. last->period <= state->period)
  452. dev_warn(pwmchip_parent(chip),
  453. ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
  454. state->period, s2.period, last->period);
  455. /*
  456. * Rounding period up is fine only if duty_cycle is 0 then, because a
  457. * flat line doesn't have a characteristic period.
  458. */
  459. if (state->period < s2.period && s2.duty_cycle)
  460. dev_warn(pwmchip_parent(chip),
  461. ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
  462. state->period, s2.period);
  463. if (last->polarity == state->polarity &&
  464. last->period == s2.period &&
  465. last->duty_cycle > s2.duty_cycle &&
  466. last->duty_cycle <= state->duty_cycle)
  467. dev_warn(pwmchip_parent(chip),
  468. ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
  469. state->duty_cycle, state->period,
  470. s2.duty_cycle, s2.period,
  471. last->duty_cycle, last->period);
  472. if (state->duty_cycle < s2.duty_cycle)
  473. dev_warn(pwmchip_parent(chip),
  474. ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
  475. state->duty_cycle, state->period,
  476. s2.duty_cycle, s2.period);
  477. /* reapply the state that the driver reported being configured. */
  478. err = chip->ops->apply(chip, pwm, &s1);
  479. trace_pwm_apply(pwm, &s1, err);
  480. if (err) {
  481. *last = s1;
  482. dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
  483. return;
  484. }
  485. *last = (struct pwm_state){ 0 };
  486. err = chip->ops->get_state(chip, pwm, last);
  487. trace_pwm_get(pwm, last, err);
  488. if (err)
  489. return;
  490. /* reapplication of the current state should give an exact match */
  491. if (s1.enabled != last->enabled ||
  492. s1.polarity != last->polarity ||
  493. (s1.enabled && s1.period != last->period) ||
  494. (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
  495. dev_err(pwmchip_parent(chip),
  496. ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
  497. s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
  498. last->enabled, last->polarity, last->duty_cycle,
  499. last->period);
  500. }
  501. }
  502. static bool pwm_state_valid(const struct pwm_state *state)
  503. {
  504. /*
  505. * For a disabled state all other state description is irrelevant and
  506. * and supposed to be ignored. So also ignore any strange values and
  507. * consider the state ok.
  508. */
  509. if (!state->enabled)
  510. return true;
  511. if (!state->period)
  512. return false;
  513. if (state->duty_cycle > state->period)
  514. return false;
  515. return true;
  516. }
  517. static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
  518. {
  519. struct pwm_chip *chip;
  520. const struct pwm_ops *ops;
  521. int err;
  522. if (!pwm || !state)
  523. return -EINVAL;
  524. if (!pwm_state_valid(state)) {
  525. /*
  526. * Allow to transition from one invalid state to another.
  527. * This ensures that you can e.g. change the polarity while
  528. * the period is zero. (This happens on stm32 when the hardware
  529. * is in its poweron default state.) This greatly simplifies
  530. * working with the sysfs API where you can only change one
  531. * parameter at a time.
  532. */
  533. if (!pwm_state_valid(&pwm->state)) {
  534. pwm->state = *state;
  535. return 0;
  536. }
  537. return -EINVAL;
  538. }
  539. chip = pwm->chip;
  540. ops = chip->ops;
  541. if (state->period == pwm->state.period &&
  542. state->duty_cycle == pwm->state.duty_cycle &&
  543. state->polarity == pwm->state.polarity &&
  544. state->enabled == pwm->state.enabled &&
  545. state->usage_power == pwm->state.usage_power)
  546. return 0;
  547. if (pwmchip_supports_waveform(chip)) {
  548. struct pwm_waveform wf;
  549. char wfhw[PWM_WFHWSIZE];
  550. BUG_ON(PWM_WFHWSIZE < ops->sizeof_wfhw);
  551. pwm_state2wf(state, &wf);
  552. /*
  553. * The rounding is wrong here for states with inverted polarity.
  554. * While .apply() rounds down duty_cycle (which represents the
  555. * time from the start of the period to the inner edge),
  556. * .round_waveform_tohw() rounds down the time the PWM is high.
  557. * Can be fixed if the need arises, until reported otherwise
  558. * let's assume that consumers don't care.
  559. */
  560. err = __pwm_round_waveform_tohw(chip, pwm, &wf, &wfhw);
  561. if (err) {
  562. if (err > 0)
  563. /*
  564. * This signals an invalid request, typically
  565. * the requested period (or duty_offset) is
  566. * smaller than possible with the hardware.
  567. */
  568. return -EINVAL;
  569. return err;
  570. }
  571. if (IS_ENABLED(CONFIG_PWM_DEBUG)) {
  572. struct pwm_waveform wf_rounded;
  573. err = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf_rounded);
  574. if (err)
  575. return err;
  576. if (!pwm_check_rounding(&wf, &wf_rounded))
  577. dev_err(&chip->dev, "Wrong rounding: requested %llu/%llu [+%llu], result %llu/%llu [+%llu]\n",
  578. wf.duty_length_ns, wf.period_length_ns, wf.duty_offset_ns,
  579. wf_rounded.duty_length_ns, wf_rounded.period_length_ns, wf_rounded.duty_offset_ns);
  580. }
  581. err = __pwm_write_waveform(chip, pwm, &wfhw);
  582. if (err)
  583. return err;
  584. pwm->state = *state;
  585. } else {
  586. err = ops->apply(chip, pwm, state);
  587. trace_pwm_apply(pwm, state, err);
  588. if (err)
  589. return err;
  590. pwm->state = *state;
  591. /*
  592. * only do this after pwm->state was applied as some
  593. * implementations of .get_state() depend on this
  594. */
  595. pwm_apply_debug(pwm, state);
  596. }
  597. return 0;
  598. }
  599. /**
  600. * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
  601. * Cannot be used in atomic context.
  602. * @pwm: PWM device
  603. * @state: new state to apply
  604. *
  605. * Returns: 0 on success, or a negative errno
  606. * Context: May sleep.
  607. */
  608. int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
  609. {
  610. int err;
  611. struct pwm_chip *chip = pwm->chip;
  612. /*
  613. * Some lowlevel driver's implementations of .apply() make use of
  614. * mutexes, also with some drivers only returning when the new
  615. * configuration is active calling pwm_apply_might_sleep() from atomic context
  616. * is a bad idea. So make it explicit that calling this function might
  617. * sleep.
  618. */
  619. might_sleep();
  620. guard(pwmchip)(chip);
  621. if (!chip->operational)
  622. return -ENODEV;
  623. if (IS_ENABLED(CONFIG_PWM_DEBUG) && chip->atomic) {
  624. /*
  625. * Catch any drivers that have been marked as atomic but
  626. * that will sleep anyway.
  627. */
  628. non_block_start();
  629. err = __pwm_apply(pwm, state);
  630. non_block_end();
  631. } else {
  632. err = __pwm_apply(pwm, state);
  633. }
  634. return err;
  635. }
  636. EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
  637. /**
  638. * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
  639. * Not all PWM devices support this function, check with pwm_might_sleep().
  640. * @pwm: PWM device
  641. * @state: new state to apply
  642. *
  643. * Returns: 0 on success, or a negative errno
  644. * Context: Any
  645. */
  646. int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
  647. {
  648. struct pwm_chip *chip = pwm->chip;
  649. WARN_ONCE(!chip->atomic,
  650. "sleeping PWM driver used in atomic context\n");
  651. guard(pwmchip)(chip);
  652. if (!chip->operational)
  653. return -ENODEV;
  654. return __pwm_apply(pwm, state);
  655. }
  656. EXPORT_SYMBOL_GPL(pwm_apply_atomic);
  657. /**
  658. * pwm_get_state_hw() - get the current PWM state from hardware
  659. * @pwm: PWM device
  660. * @state: state to fill with the current PWM state
  661. *
  662. * Similar to pwm_get_state() but reads the current PWM state from hardware
  663. * instead of the requested state.
  664. *
  665. * Returns: 0 on success or a negative error code on failure.
  666. * Context: May sleep.
  667. */
  668. int pwm_get_state_hw(struct pwm_device *pwm, struct pwm_state *state)
  669. {
  670. struct pwm_chip *chip = pwm->chip;
  671. const struct pwm_ops *ops = chip->ops;
  672. int ret = -EOPNOTSUPP;
  673. might_sleep();
  674. guard(pwmchip)(chip);
  675. if (!chip->operational)
  676. return -ENODEV;
  677. if (pwmchip_supports_waveform(chip) && ops->read_waveform) {
  678. char wfhw[PWM_WFHWSIZE];
  679. struct pwm_waveform wf;
  680. BUG_ON(PWM_WFHWSIZE < ops->sizeof_wfhw);
  681. ret = __pwm_read_waveform(chip, pwm, &wfhw);
  682. if (ret)
  683. return ret;
  684. ret = __pwm_round_waveform_fromhw(chip, pwm, &wfhw, &wf);
  685. if (ret)
  686. return ret;
  687. pwm_wf2state(&wf, state);
  688. } else if (ops->get_state) {
  689. ret = ops->get_state(chip, pwm, state);
  690. trace_pwm_get(pwm, state, ret);
  691. }
  692. return ret;
  693. }
  694. EXPORT_SYMBOL_GPL(pwm_get_state_hw);
  695. /**
  696. * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
  697. * @pwm: PWM device
  698. *
  699. * This function will adjust the PWM config to the PWM arguments provided
  700. * by the DT or PWM lookup table. This is particularly useful to adapt
  701. * the bootloader config to the Linux one.
  702. *
  703. * Returns: 0 on success or a negative error code on failure.
  704. * Context: May sleep.
  705. */
  706. int pwm_adjust_config(struct pwm_device *pwm)
  707. {
  708. struct pwm_state state;
  709. struct pwm_args pargs;
  710. pwm_get_args(pwm, &pargs);
  711. pwm_get_state(pwm, &state);
  712. /*
  713. * If the current period is zero it means that either the PWM driver
  714. * does not support initial state retrieval or the PWM has not yet
  715. * been configured.
  716. *
  717. * In either case, we setup the new period and polarity, and assign a
  718. * duty cycle of 0.
  719. */
  720. if (!state.period) {
  721. state.duty_cycle = 0;
  722. state.period = pargs.period;
  723. state.polarity = pargs.polarity;
  724. return pwm_apply_might_sleep(pwm, &state);
  725. }
  726. /*
  727. * Adjust the PWM duty cycle/period based on the period value provided
  728. * in PWM args.
  729. */
  730. if (pargs.period != state.period) {
  731. u64 dutycycle = (u64)state.duty_cycle * pargs.period;
  732. do_div(dutycycle, state.period);
  733. state.duty_cycle = dutycycle;
  734. state.period = pargs.period;
  735. }
  736. /*
  737. * If the polarity changed, we should also change the duty cycle.
  738. */
  739. if (pargs.polarity != state.polarity) {
  740. state.polarity = pargs.polarity;
  741. state.duty_cycle = state.period - state.duty_cycle;
  742. }
  743. return pwm_apply_might_sleep(pwm, &state);
  744. }
  745. EXPORT_SYMBOL_GPL(pwm_adjust_config);
  746. /**
  747. * pwm_capture() - capture and report a PWM signal
  748. * @pwm: PWM device
  749. * @result: structure to fill with capture result
  750. * @timeout: time to wait, in milliseconds, before giving up on capture
  751. *
  752. * Returns: 0 on success or a negative error code on failure.
  753. */
  754. static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
  755. unsigned long timeout)
  756. {
  757. struct pwm_chip *chip = pwm->chip;
  758. const struct pwm_ops *ops = chip->ops;
  759. if (!ops->capture)
  760. return -ENOSYS;
  761. /*
  762. * Holding the pwm_lock is probably not needed. If you use pwm_capture()
  763. * and you're interested to speed it up, please convince yourself it's
  764. * really not needed, test and then suggest a patch on the mailing list.
  765. */
  766. guard(mutex)(&pwm_lock);
  767. guard(pwmchip)(chip);
  768. if (!chip->operational)
  769. return -ENODEV;
  770. return ops->capture(chip, pwm, result, timeout);
  771. }
  772. static struct pwm_chip *pwmchip_find_by_name(const char *name)
  773. {
  774. struct pwm_chip *chip;
  775. unsigned long id, tmp;
  776. if (!name)
  777. return NULL;
  778. guard(mutex)(&pwm_lock);
  779. idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
  780. if (device_match_name(pwmchip_parent(chip), name))
  781. return chip;
  782. }
  783. return NULL;
  784. }
  785. static int pwm_device_request(struct pwm_device *pwm, const char *label)
  786. {
  787. int err;
  788. struct pwm_chip *chip = pwm->chip;
  789. const struct pwm_ops *ops = chip->ops;
  790. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  791. return -EBUSY;
  792. /*
  793. * This function is called while holding pwm_lock. As .operational only
  794. * changes while holding this lock, checking it here without holding the
  795. * chip lock is fine.
  796. */
  797. if (!chip->operational)
  798. return -ENODEV;
  799. if (!try_module_get(chip->owner))
  800. return -ENODEV;
  801. if (!get_device(&chip->dev)) {
  802. err = -ENODEV;
  803. goto err_get_device;
  804. }
  805. if (ops->request) {
  806. err = ops->request(chip, pwm);
  807. if (err) {
  808. put_device(&chip->dev);
  809. err_get_device:
  810. module_put(chip->owner);
  811. return err;
  812. }
  813. }
  814. if (ops->read_waveform || ops->get_state) {
  815. /*
  816. * Zero-initialize state because most drivers are unaware of
  817. * .usage_power. The other members of state are supposed to be
  818. * set by lowlevel drivers. We still initialize the whole
  819. * structure for simplicity even though this might paper over
  820. * faulty implementations of .get_state().
  821. */
  822. struct pwm_state state = { 0, };
  823. err = pwm_get_state_hw(pwm, &state);
  824. if (!err)
  825. pwm->state = state;
  826. if (IS_ENABLED(CONFIG_PWM_DEBUG))
  827. pwm->last = pwm->state;
  828. }
  829. set_bit(PWMF_REQUESTED, &pwm->flags);
  830. pwm->label = label;
  831. return 0;
  832. }
  833. /**
  834. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  835. * @chip: PWM chip
  836. * @index: per-chip index of the PWM to request
  837. * @label: a literal description string of this PWM
  838. *
  839. * Returns: A pointer to the PWM device at the given index of the given PWM
  840. * chip. A negative error code is returned if the index is not valid for the
  841. * specified PWM chip or if the PWM device cannot be requested.
  842. */
  843. static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  844. unsigned int index,
  845. const char *label)
  846. {
  847. struct pwm_device *pwm;
  848. int err;
  849. if (!chip || index >= chip->npwm)
  850. return ERR_PTR(-EINVAL);
  851. guard(mutex)(&pwm_lock);
  852. pwm = &chip->pwms[index];
  853. err = pwm_device_request(pwm, label);
  854. if (err < 0)
  855. return ERR_PTR(err);
  856. return pwm;
  857. }
  858. struct pwm_device *
  859. of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
  860. {
  861. struct pwm_device *pwm;
  862. /* period in the second cell and flags in the third cell are optional */
  863. if (args->args_count < 1)
  864. return ERR_PTR(-EINVAL);
  865. pwm = pwm_request_from_chip(chip, args->args[0], NULL);
  866. if (IS_ERR(pwm))
  867. return pwm;
  868. if (args->args_count > 1)
  869. pwm->args.period = args->args[1];
  870. pwm->args.polarity = PWM_POLARITY_NORMAL;
  871. if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
  872. pwm->args.polarity = PWM_POLARITY_INVERSED;
  873. return pwm;
  874. }
  875. EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
  876. /*
  877. * This callback is used for PXA PWM chips that only have a single PWM line.
  878. * For such chips you could argue that passing the line number (i.e. the first
  879. * parameter in the common case) is useless as it's always zero. So compared to
  880. * the default xlate function of_pwm_xlate_with_flags() the first parameter is
  881. * the default period and the second are flags.
  882. *
  883. * Note that if #pwm-cells = <3>, the semantic is the same as for
  884. * of_pwm_xlate_with_flags() to allow converting the affected driver to
  885. * #pwm-cells = <3> without breaking the legacy binding.
  886. *
  887. * Don't use for new drivers.
  888. */
  889. struct pwm_device *
  890. of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
  891. {
  892. struct pwm_device *pwm;
  893. if (args->args_count >= 3)
  894. return of_pwm_xlate_with_flags(chip, args);
  895. pwm = pwm_request_from_chip(chip, 0, NULL);
  896. if (IS_ERR(pwm))
  897. return pwm;
  898. if (args->args_count > 0)
  899. pwm->args.period = args->args[0];
  900. pwm->args.polarity = PWM_POLARITY_NORMAL;
  901. if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
  902. pwm->args.polarity = PWM_POLARITY_INVERSED;
  903. return pwm;
  904. }
  905. EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
  906. struct pwm_export {
  907. struct device pwm_dev;
  908. struct pwm_device *pwm;
  909. struct mutex lock;
  910. struct pwm_state suspend;
  911. };
  912. static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
  913. {
  914. return container_of(pwmchip_dev, struct pwm_chip, dev);
  915. }
  916. static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
  917. {
  918. return container_of(pwm_dev, struct pwm_export, pwm_dev);
  919. }
  920. static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
  921. {
  922. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  923. return export->pwm;
  924. }
  925. static ssize_t period_show(struct device *pwm_dev,
  926. struct device_attribute *attr,
  927. char *buf)
  928. {
  929. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  930. struct pwm_state state;
  931. pwm_get_state(pwm, &state);
  932. return sysfs_emit(buf, "%llu\n", state.period);
  933. }
  934. static ssize_t period_store(struct device *pwm_dev,
  935. struct device_attribute *attr,
  936. const char *buf, size_t size)
  937. {
  938. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  939. struct pwm_device *pwm = export->pwm;
  940. struct pwm_state state;
  941. u64 val;
  942. int ret;
  943. ret = kstrtou64(buf, 0, &val);
  944. if (ret)
  945. return ret;
  946. guard(mutex)(&export->lock);
  947. pwm_get_state(pwm, &state);
  948. state.period = val;
  949. ret = pwm_apply_might_sleep(pwm, &state);
  950. return ret ? : size;
  951. }
  952. static ssize_t duty_cycle_show(struct device *pwm_dev,
  953. struct device_attribute *attr,
  954. char *buf)
  955. {
  956. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  957. struct pwm_state state;
  958. pwm_get_state(pwm, &state);
  959. return sysfs_emit(buf, "%llu\n", state.duty_cycle);
  960. }
  961. static ssize_t duty_cycle_store(struct device *pwm_dev,
  962. struct device_attribute *attr,
  963. const char *buf, size_t size)
  964. {
  965. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  966. struct pwm_device *pwm = export->pwm;
  967. struct pwm_state state;
  968. u64 val;
  969. int ret;
  970. ret = kstrtou64(buf, 0, &val);
  971. if (ret)
  972. return ret;
  973. guard(mutex)(&export->lock);
  974. pwm_get_state(pwm, &state);
  975. state.duty_cycle = val;
  976. ret = pwm_apply_might_sleep(pwm, &state);
  977. return ret ? : size;
  978. }
  979. static ssize_t enable_show(struct device *pwm_dev,
  980. struct device_attribute *attr,
  981. char *buf)
  982. {
  983. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  984. struct pwm_state state;
  985. pwm_get_state(pwm, &state);
  986. return sysfs_emit(buf, "%d\n", state.enabled);
  987. }
  988. static ssize_t enable_store(struct device *pwm_dev,
  989. struct device_attribute *attr,
  990. const char *buf, size_t size)
  991. {
  992. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  993. struct pwm_device *pwm = export->pwm;
  994. struct pwm_state state;
  995. int val, ret;
  996. ret = kstrtoint(buf, 0, &val);
  997. if (ret)
  998. return ret;
  999. guard(mutex)(&export->lock);
  1000. pwm_get_state(pwm, &state);
  1001. switch (val) {
  1002. case 0:
  1003. state.enabled = false;
  1004. break;
  1005. case 1:
  1006. state.enabled = true;
  1007. break;
  1008. default:
  1009. return -EINVAL;
  1010. }
  1011. ret = pwm_apply_might_sleep(pwm, &state);
  1012. return ret ? : size;
  1013. }
  1014. static ssize_t polarity_show(struct device *pwm_dev,
  1015. struct device_attribute *attr,
  1016. char *buf)
  1017. {
  1018. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  1019. const char *polarity = "unknown";
  1020. struct pwm_state state;
  1021. pwm_get_state(pwm, &state);
  1022. switch (state.polarity) {
  1023. case PWM_POLARITY_NORMAL:
  1024. polarity = "normal";
  1025. break;
  1026. case PWM_POLARITY_INVERSED:
  1027. polarity = "inversed";
  1028. break;
  1029. }
  1030. return sysfs_emit(buf, "%s\n", polarity);
  1031. }
  1032. static ssize_t polarity_store(struct device *pwm_dev,
  1033. struct device_attribute *attr,
  1034. const char *buf, size_t size)
  1035. {
  1036. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  1037. struct pwm_device *pwm = export->pwm;
  1038. enum pwm_polarity polarity;
  1039. struct pwm_state state;
  1040. int ret;
  1041. if (sysfs_streq(buf, "normal"))
  1042. polarity = PWM_POLARITY_NORMAL;
  1043. else if (sysfs_streq(buf, "inversed"))
  1044. polarity = PWM_POLARITY_INVERSED;
  1045. else
  1046. return -EINVAL;
  1047. guard(mutex)(&export->lock);
  1048. pwm_get_state(pwm, &state);
  1049. state.polarity = polarity;
  1050. ret = pwm_apply_might_sleep(pwm, &state);
  1051. return ret ? : size;
  1052. }
  1053. static ssize_t capture_show(struct device *pwm_dev,
  1054. struct device_attribute *attr,
  1055. char *buf)
  1056. {
  1057. struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  1058. struct pwm_capture result;
  1059. int ret;
  1060. ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
  1061. if (ret)
  1062. return ret;
  1063. return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
  1064. }
  1065. static DEVICE_ATTR_RW(period);
  1066. static DEVICE_ATTR_RW(duty_cycle);
  1067. static DEVICE_ATTR_RW(enable);
  1068. static DEVICE_ATTR_RW(polarity);
  1069. static DEVICE_ATTR_RO(capture);
  1070. static struct attribute *pwm_attrs[] = {
  1071. &dev_attr_period.attr,
  1072. &dev_attr_duty_cycle.attr,
  1073. &dev_attr_enable.attr,
  1074. &dev_attr_polarity.attr,
  1075. &dev_attr_capture.attr,
  1076. NULL
  1077. };
  1078. ATTRIBUTE_GROUPS(pwm);
  1079. static void pwm_export_release(struct device *pwm_dev)
  1080. {
  1081. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  1082. kfree(export);
  1083. }
  1084. static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
  1085. {
  1086. struct pwm_export *export;
  1087. char *pwm_prop[2];
  1088. int ret;
  1089. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  1090. return -EBUSY;
  1091. export = kzalloc_obj(*export);
  1092. if (!export) {
  1093. clear_bit(PWMF_EXPORTED, &pwm->flags);
  1094. return -ENOMEM;
  1095. }
  1096. export->pwm = pwm;
  1097. mutex_init(&export->lock);
  1098. export->pwm_dev.release = pwm_export_release;
  1099. export->pwm_dev.parent = pwmchip_dev;
  1100. export->pwm_dev.devt = MKDEV(0, 0);
  1101. export->pwm_dev.groups = pwm_groups;
  1102. dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
  1103. ret = device_register(&export->pwm_dev);
  1104. if (ret) {
  1105. clear_bit(PWMF_EXPORTED, &pwm->flags);
  1106. put_device(&export->pwm_dev);
  1107. export = NULL;
  1108. return ret;
  1109. }
  1110. pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
  1111. pwm_prop[1] = NULL;
  1112. kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
  1113. kfree(pwm_prop[0]);
  1114. return 0;
  1115. }
  1116. static int pwm_unexport_match(struct device *pwm_dev, const void *data)
  1117. {
  1118. return pwm_from_dev(pwm_dev) == data;
  1119. }
  1120. static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
  1121. {
  1122. struct device *pwm_dev;
  1123. char *pwm_prop[2];
  1124. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  1125. return -ENODEV;
  1126. pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
  1127. if (!pwm_dev)
  1128. return -ENODEV;
  1129. pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
  1130. pwm_prop[1] = NULL;
  1131. kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
  1132. kfree(pwm_prop[0]);
  1133. /* for device_find_child() */
  1134. put_device(pwm_dev);
  1135. device_unregister(pwm_dev);
  1136. pwm_put(pwm);
  1137. return 0;
  1138. }
  1139. static ssize_t export_store(struct device *pwmchip_dev,
  1140. struct device_attribute *attr,
  1141. const char *buf, size_t len)
  1142. {
  1143. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  1144. struct pwm_device *pwm;
  1145. unsigned int hwpwm;
  1146. int ret;
  1147. ret = kstrtouint(buf, 0, &hwpwm);
  1148. if (ret < 0)
  1149. return ret;
  1150. if (hwpwm >= chip->npwm)
  1151. return -ENODEV;
  1152. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  1153. if (IS_ERR(pwm))
  1154. return PTR_ERR(pwm);
  1155. ret = pwm_export_child(pwmchip_dev, pwm);
  1156. if (ret < 0)
  1157. pwm_put(pwm);
  1158. return ret ? : len;
  1159. }
  1160. static DEVICE_ATTR_WO(export);
  1161. static ssize_t unexport_store(struct device *pwmchip_dev,
  1162. struct device_attribute *attr,
  1163. const char *buf, size_t len)
  1164. {
  1165. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  1166. unsigned int hwpwm;
  1167. int ret;
  1168. ret = kstrtouint(buf, 0, &hwpwm);
  1169. if (ret < 0)
  1170. return ret;
  1171. if (hwpwm >= chip->npwm)
  1172. return -ENODEV;
  1173. ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
  1174. return ret ? : len;
  1175. }
  1176. static DEVICE_ATTR_WO(unexport);
  1177. static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
  1178. char *buf)
  1179. {
  1180. const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  1181. return sysfs_emit(buf, "%u\n", chip->npwm);
  1182. }
  1183. static DEVICE_ATTR_RO(npwm);
  1184. static struct attribute *pwm_chip_attrs[] = {
  1185. &dev_attr_export.attr,
  1186. &dev_attr_unexport.attr,
  1187. &dev_attr_npwm.attr,
  1188. NULL,
  1189. };
  1190. ATTRIBUTE_GROUPS(pwm_chip);
  1191. /* takes export->lock on success */
  1192. static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
  1193. struct pwm_device *pwm,
  1194. struct pwm_state *state)
  1195. {
  1196. struct device *pwm_dev;
  1197. struct pwm_export *export;
  1198. if (!test_bit(PWMF_EXPORTED, &pwm->flags))
  1199. return NULL;
  1200. pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
  1201. if (!pwm_dev)
  1202. return NULL;
  1203. export = pwmexport_from_dev(pwm_dev);
  1204. put_device(pwm_dev); /* for device_find_child() */
  1205. mutex_lock(&export->lock);
  1206. pwm_get_state(pwm, state);
  1207. return export;
  1208. }
  1209. static int pwm_class_apply_state(struct pwm_export *export,
  1210. struct pwm_device *pwm,
  1211. struct pwm_state *state)
  1212. {
  1213. int ret = pwm_apply_might_sleep(pwm, state);
  1214. /* release lock taken in pwm_class_get_state */
  1215. mutex_unlock(&export->lock);
  1216. return ret;
  1217. }
  1218. static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
  1219. {
  1220. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  1221. unsigned int i;
  1222. int ret = 0;
  1223. for (i = 0; i < npwm; i++) {
  1224. struct pwm_device *pwm = &chip->pwms[i];
  1225. struct pwm_state state;
  1226. struct pwm_export *export;
  1227. export = pwm_class_get_state(pwmchip_dev, pwm, &state);
  1228. if (!export)
  1229. continue;
  1230. /* If pwmchip was not enabled before suspend, do nothing. */
  1231. if (!export->suspend.enabled) {
  1232. /* release lock taken in pwm_class_get_state */
  1233. mutex_unlock(&export->lock);
  1234. continue;
  1235. }
  1236. state.enabled = export->suspend.enabled;
  1237. ret = pwm_class_apply_state(export, pwm, &state);
  1238. if (ret < 0)
  1239. break;
  1240. }
  1241. return ret;
  1242. }
  1243. static int pwm_class_suspend(struct device *pwmchip_dev)
  1244. {
  1245. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  1246. unsigned int i;
  1247. int ret = 0;
  1248. for (i = 0; i < chip->npwm; i++) {
  1249. struct pwm_device *pwm = &chip->pwms[i];
  1250. struct pwm_state state;
  1251. struct pwm_export *export;
  1252. export = pwm_class_get_state(pwmchip_dev, pwm, &state);
  1253. if (!export)
  1254. continue;
  1255. /*
  1256. * If pwmchip was not enabled before suspend, save
  1257. * state for resume time and do nothing else.
  1258. */
  1259. export->suspend = state;
  1260. if (!state.enabled) {
  1261. /* release lock taken in pwm_class_get_state */
  1262. mutex_unlock(&export->lock);
  1263. continue;
  1264. }
  1265. state.enabled = false;
  1266. ret = pwm_class_apply_state(export, pwm, &state);
  1267. if (ret < 0) {
  1268. /*
  1269. * roll back the PWM devices that were disabled by
  1270. * this suspend function.
  1271. */
  1272. pwm_class_resume_npwm(pwmchip_dev, i);
  1273. break;
  1274. }
  1275. }
  1276. return ret;
  1277. }
  1278. static int pwm_class_resume(struct device *pwmchip_dev)
  1279. {
  1280. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  1281. return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
  1282. }
  1283. static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
  1284. static struct class pwm_class = {
  1285. .name = "pwm",
  1286. .dev_groups = pwm_chip_groups,
  1287. .pm = pm_sleep_ptr(&pwm_class_pm_ops),
  1288. };
  1289. static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  1290. {
  1291. unsigned int i;
  1292. for (i = 0; i < chip->npwm; i++) {
  1293. struct pwm_device *pwm = &chip->pwms[i];
  1294. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  1295. pwm_unexport_child(&chip->dev, pwm);
  1296. }
  1297. }
  1298. #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
  1299. static void *pwmchip_priv(struct pwm_chip *chip)
  1300. {
  1301. return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
  1302. }
  1303. /* This is the counterpart to pwmchip_alloc() */
  1304. void pwmchip_put(struct pwm_chip *chip)
  1305. {
  1306. put_device(&chip->dev);
  1307. }
  1308. EXPORT_SYMBOL_GPL(pwmchip_put);
  1309. void pwmchip_release(struct device *pwmchip_dev)
  1310. {
  1311. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  1312. kfree(chip);
  1313. }
  1314. EXPORT_SYMBOL_GPL(pwmchip_release);
  1315. struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
  1316. {
  1317. struct pwm_chip *chip;
  1318. struct device *pwmchip_dev;
  1319. size_t alloc_size;
  1320. unsigned int i;
  1321. alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
  1322. sizeof_priv);
  1323. chip = kzalloc(alloc_size, GFP_KERNEL);
  1324. if (!chip)
  1325. return ERR_PTR(-ENOMEM);
  1326. chip->npwm = npwm;
  1327. chip->uses_pwmchip_alloc = true;
  1328. chip->operational = false;
  1329. pwmchip_dev = &chip->dev;
  1330. device_initialize(pwmchip_dev);
  1331. pwmchip_dev->class = &pwm_class;
  1332. pwmchip_dev->parent = parent;
  1333. pwmchip_dev->release = pwmchip_release;
  1334. pwmchip_set_drvdata(chip, pwmchip_priv(chip));
  1335. for (i = 0; i < chip->npwm; i++) {
  1336. struct pwm_device *pwm = &chip->pwms[i];
  1337. pwm->chip = chip;
  1338. pwm->hwpwm = i;
  1339. }
  1340. return chip;
  1341. }
  1342. EXPORT_SYMBOL_GPL(pwmchip_alloc);
  1343. static void devm_pwmchip_put(void *data)
  1344. {
  1345. struct pwm_chip *chip = data;
  1346. pwmchip_put(chip);
  1347. }
  1348. struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
  1349. {
  1350. struct pwm_chip *chip;
  1351. int ret;
  1352. chip = pwmchip_alloc(parent, npwm, sizeof_priv);
  1353. if (IS_ERR(chip))
  1354. return chip;
  1355. ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
  1356. if (ret)
  1357. return ERR_PTR(ret);
  1358. return chip;
  1359. }
  1360. EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
  1361. static void of_pwmchip_add(struct pwm_chip *chip)
  1362. {
  1363. if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
  1364. return;
  1365. if (!chip->of_xlate)
  1366. chip->of_xlate = of_pwm_xlate_with_flags;
  1367. of_node_get(pwmchip_parent(chip)->of_node);
  1368. }
  1369. static void of_pwmchip_remove(struct pwm_chip *chip)
  1370. {
  1371. if (pwmchip_parent(chip))
  1372. of_node_put(pwmchip_parent(chip)->of_node);
  1373. }
  1374. static bool pwm_ops_check(const struct pwm_chip *chip)
  1375. {
  1376. const struct pwm_ops *ops = chip->ops;
  1377. if (ops->write_waveform) {
  1378. if (!ops->round_waveform_tohw ||
  1379. !ops->round_waveform_fromhw)
  1380. return false;
  1381. if (PWM_WFHWSIZE < ops->sizeof_wfhw) {
  1382. dev_warn(pwmchip_parent(chip), "PWM_WFHWSIZE < %zu\n", ops->sizeof_wfhw);
  1383. return false;
  1384. }
  1385. } else {
  1386. if (!ops->apply)
  1387. return false;
  1388. if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
  1389. dev_warn(pwmchip_parent(chip),
  1390. "Please implement the .get_state() callback\n");
  1391. }
  1392. return true;
  1393. }
  1394. static struct device_link *pwm_device_link_add(struct device *dev,
  1395. struct pwm_device *pwm)
  1396. {
  1397. struct device_link *dl;
  1398. if (!dev) {
  1399. /*
  1400. * No device for the PWM consumer has been provided. It may
  1401. * impact the PM sequence ordering: the PWM supplier may get
  1402. * suspended before the consumer.
  1403. */
  1404. dev_warn(pwmchip_parent(pwm->chip),
  1405. "No consumer device specified to create a link to\n");
  1406. return NULL;
  1407. }
  1408. dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
  1409. if (!dl) {
  1410. dev_err(dev, "failed to create device link to %s\n",
  1411. dev_name(pwmchip_parent(pwm->chip)));
  1412. return ERR_PTR(-EINVAL);
  1413. }
  1414. return dl;
  1415. }
  1416. static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
  1417. {
  1418. struct pwm_chip *chip;
  1419. unsigned long id, tmp;
  1420. guard(mutex)(&pwm_lock);
  1421. idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
  1422. if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode))
  1423. return chip;
  1424. return ERR_PTR(-EPROBE_DEFER);
  1425. }
  1426. /**
  1427. * of_pwm_get() - request a PWM via the PWM framework
  1428. * @dev: device for PWM consumer
  1429. * @np: device node to get the PWM from
  1430. * @con_id: consumer name
  1431. *
  1432. * Returns the PWM device parsed from the phandle and index specified in the
  1433. * "pwms" property of a device tree node or a negative error-code on failure.
  1434. * Values parsed from the device tree are stored in the returned PWM device
  1435. * object.
  1436. *
  1437. * If con_id is NULL, the first PWM device listed in the "pwms" property will
  1438. * be requested. Otherwise the "pwm-names" property is used to do a reverse
  1439. * lookup of the PWM index. This also means that the "pwm-names" property
  1440. * becomes mandatory for devices that look up the PWM device via the con_id
  1441. * parameter.
  1442. *
  1443. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1444. * error code on failure.
  1445. */
  1446. static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
  1447. const char *con_id)
  1448. {
  1449. struct pwm_device *pwm = NULL;
  1450. struct of_phandle_args args;
  1451. struct device_link *dl;
  1452. struct pwm_chip *chip;
  1453. int index = 0;
  1454. int err;
  1455. if (con_id) {
  1456. index = of_property_match_string(np, "pwm-names", con_id);
  1457. if (index < 0)
  1458. return ERR_PTR(index);
  1459. }
  1460. err = of_parse_phandle_with_args_map(np, "pwms", "pwm", index, &args);
  1461. if (err) {
  1462. pr_err("%s(): can't parse \"pwms\" property\n", __func__);
  1463. return ERR_PTR(err);
  1464. }
  1465. chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
  1466. if (IS_ERR(chip)) {
  1467. if (PTR_ERR(chip) != -EPROBE_DEFER)
  1468. pr_err("%s(): PWM chip not found\n", __func__);
  1469. pwm = ERR_CAST(chip);
  1470. goto put;
  1471. }
  1472. pwm = chip->of_xlate(chip, &args);
  1473. if (IS_ERR(pwm))
  1474. goto put;
  1475. dl = pwm_device_link_add(dev, pwm);
  1476. if (IS_ERR(dl)) {
  1477. /* of_xlate ended up calling pwm_request_from_chip() */
  1478. pwm_put(pwm);
  1479. pwm = ERR_CAST(dl);
  1480. goto put;
  1481. }
  1482. /*
  1483. * If a consumer name was not given, try to look it up from the
  1484. * "pwm-names" property if it exists. Otherwise use the name of
  1485. * the user device node.
  1486. */
  1487. if (!con_id) {
  1488. err = of_property_read_string_index(np, "pwm-names", index,
  1489. &con_id);
  1490. if (err < 0)
  1491. con_id = np->name;
  1492. }
  1493. pwm->label = con_id;
  1494. put:
  1495. of_node_put(args.np);
  1496. return pwm;
  1497. }
  1498. /**
  1499. * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
  1500. * @fwnode: firmware node to get the "pwms" property from
  1501. *
  1502. * Returns the PWM device parsed from the fwnode and index specified in the
  1503. * "pwms" property or a negative error-code on failure.
  1504. * Values parsed from the device tree are stored in the returned PWM device
  1505. * object.
  1506. *
  1507. * This is analogous to of_pwm_get() except con_id is not yet supported.
  1508. * ACPI entries must look like
  1509. * Package () {"pwms", Package ()
  1510. * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
  1511. *
  1512. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1513. * error code on failure.
  1514. */
  1515. static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
  1516. {
  1517. struct pwm_device *pwm;
  1518. struct fwnode_reference_args args;
  1519. struct pwm_chip *chip;
  1520. int ret;
  1521. memset(&args, 0, sizeof(args));
  1522. ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
  1523. if (ret < 0)
  1524. return ERR_PTR(ret);
  1525. if (args.nargs < 2)
  1526. return ERR_PTR(-EPROTO);
  1527. chip = fwnode_to_pwmchip(args.fwnode);
  1528. if (IS_ERR(chip))
  1529. return ERR_CAST(chip);
  1530. pwm = pwm_request_from_chip(chip, args.args[0], NULL);
  1531. if (IS_ERR(pwm))
  1532. return pwm;
  1533. pwm->args.period = args.args[1];
  1534. pwm->args.polarity = PWM_POLARITY_NORMAL;
  1535. if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
  1536. pwm->args.polarity = PWM_POLARITY_INVERSED;
  1537. return pwm;
  1538. }
  1539. static DEFINE_MUTEX(pwm_lookup_lock);
  1540. static LIST_HEAD(pwm_lookup_list);
  1541. /**
  1542. * pwm_get() - look up and request a PWM device
  1543. * @dev: device for PWM consumer
  1544. * @con_id: consumer name
  1545. *
  1546. * Lookup is first attempted using DT. If the device was not instantiated from
  1547. * a device tree, a PWM chip and a relative index is looked up via a table
  1548. * supplied by board setup code (see pwm_add_table()).
  1549. *
  1550. * Once a PWM chip has been found the specified PWM device will be requested
  1551. * and is ready to be used.
  1552. *
  1553. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1554. * error code on failure.
  1555. */
  1556. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  1557. {
  1558. const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
  1559. const char *dev_id = dev ? dev_name(dev) : NULL;
  1560. struct pwm_device *pwm;
  1561. struct pwm_chip *chip;
  1562. struct device_link *dl;
  1563. unsigned int best = 0;
  1564. struct pwm_lookup *p, *chosen = NULL;
  1565. unsigned int match;
  1566. int err;
  1567. /* look up via DT first */
  1568. if (is_of_node(fwnode))
  1569. return of_pwm_get(dev, to_of_node(fwnode), con_id);
  1570. /* then lookup via ACPI */
  1571. if (is_acpi_node(fwnode)) {
  1572. pwm = acpi_pwm_get(fwnode);
  1573. if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
  1574. return pwm;
  1575. }
  1576. /*
  1577. * We look up the provider in the static table typically provided by
  1578. * board setup code. We first try to lookup the consumer device by
  1579. * name. If the consumer device was passed in as NULL or if no match
  1580. * was found, we try to find the consumer by directly looking it up
  1581. * by name.
  1582. *
  1583. * If a match is found, the provider PWM chip is looked up by name
  1584. * and a PWM device is requested using the PWM device per-chip index.
  1585. *
  1586. * The lookup algorithm was shamelessly taken from the clock
  1587. * framework:
  1588. *
  1589. * We do slightly fuzzy matching here:
  1590. * An entry with a NULL ID is assumed to be a wildcard.
  1591. * If an entry has a device ID, it must match
  1592. * If an entry has a connection ID, it must match
  1593. * Then we take the most specific entry - with the following order
  1594. * of precedence: dev+con > dev only > con only.
  1595. */
  1596. scoped_guard(mutex, &pwm_lookup_lock)
  1597. list_for_each_entry(p, &pwm_lookup_list, list) {
  1598. match = 0;
  1599. if (p->dev_id) {
  1600. if (!dev_id || strcmp(p->dev_id, dev_id))
  1601. continue;
  1602. match += 2;
  1603. }
  1604. if (p->con_id) {
  1605. if (!con_id || strcmp(p->con_id, con_id))
  1606. continue;
  1607. match += 1;
  1608. }
  1609. if (match > best) {
  1610. chosen = p;
  1611. if (match != 3)
  1612. best = match;
  1613. else
  1614. break;
  1615. }
  1616. }
  1617. if (!chosen)
  1618. return ERR_PTR(-ENODEV);
  1619. chip = pwmchip_find_by_name(chosen->provider);
  1620. /*
  1621. * If the lookup entry specifies a module, load the module and retry
  1622. * the PWM chip lookup. This can be used to work around driver load
  1623. * ordering issues if driver's can't be made to properly support the
  1624. * deferred probe mechanism.
  1625. */
  1626. if (!chip && chosen->module) {
  1627. err = request_module(chosen->module);
  1628. if (err == 0)
  1629. chip = pwmchip_find_by_name(chosen->provider);
  1630. }
  1631. if (!chip)
  1632. return ERR_PTR(-EPROBE_DEFER);
  1633. pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
  1634. if (IS_ERR(pwm))
  1635. return pwm;
  1636. dl = pwm_device_link_add(dev, pwm);
  1637. if (IS_ERR(dl)) {
  1638. pwm_put(pwm);
  1639. return ERR_CAST(dl);
  1640. }
  1641. pwm->args.period = chosen->period;
  1642. pwm->args.polarity = chosen->polarity;
  1643. return pwm;
  1644. }
  1645. EXPORT_SYMBOL_GPL(pwm_get);
  1646. static void __pwm_put(struct pwm_device *pwm)
  1647. {
  1648. struct pwm_chip *chip = pwm->chip;
  1649. /*
  1650. * Trigger a warning if a consumer called pwm_put() twice.
  1651. * If the chip isn't operational, PWMF_REQUESTED was already cleared in
  1652. * pwmchip_remove(). So don't warn in this case.
  1653. */
  1654. if (chip->operational && !test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  1655. pr_warn("PWM device already freed\n");
  1656. return;
  1657. }
  1658. if (chip->operational && chip->ops->free)
  1659. pwm->chip->ops->free(pwm->chip, pwm);
  1660. pwm->label = NULL;
  1661. put_device(&chip->dev);
  1662. module_put(chip->owner);
  1663. }
  1664. /**
  1665. * pwm_put() - release a PWM device
  1666. * @pwm: PWM device
  1667. */
  1668. void pwm_put(struct pwm_device *pwm)
  1669. {
  1670. if (!pwm)
  1671. return;
  1672. guard(mutex)(&pwm_lock);
  1673. __pwm_put(pwm);
  1674. }
  1675. EXPORT_SYMBOL_GPL(pwm_put);
  1676. static void devm_pwm_release(void *pwm)
  1677. {
  1678. pwm_put(pwm);
  1679. }
  1680. /**
  1681. * devm_pwm_get() - resource managed pwm_get()
  1682. * @dev: device for PWM consumer
  1683. * @con_id: consumer name
  1684. *
  1685. * This function performs like pwm_get() but the acquired PWM device will
  1686. * automatically be released on driver detach.
  1687. *
  1688. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1689. * error code on failure.
  1690. */
  1691. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
  1692. {
  1693. struct pwm_device *pwm;
  1694. int ret;
  1695. pwm = pwm_get(dev, con_id);
  1696. if (IS_ERR(pwm))
  1697. return pwm;
  1698. ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
  1699. if (ret)
  1700. return ERR_PTR(ret);
  1701. return pwm;
  1702. }
  1703. EXPORT_SYMBOL_GPL(devm_pwm_get);
  1704. /**
  1705. * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
  1706. * @dev: device for PWM consumer
  1707. * @fwnode: firmware node to get the PWM from
  1708. * @con_id: consumer name
  1709. *
  1710. * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
  1711. * acpi_pwm_get() for a detailed description.
  1712. *
  1713. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1714. * error code on failure.
  1715. */
  1716. struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
  1717. struct fwnode_handle *fwnode,
  1718. const char *con_id)
  1719. {
  1720. struct pwm_device *pwm = ERR_PTR(-ENODEV);
  1721. int ret;
  1722. if (is_of_node(fwnode))
  1723. pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
  1724. else if (is_acpi_node(fwnode))
  1725. pwm = acpi_pwm_get(fwnode);
  1726. if (IS_ERR(pwm))
  1727. return pwm;
  1728. ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
  1729. if (ret)
  1730. return ERR_PTR(ret);
  1731. return pwm;
  1732. }
  1733. EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
  1734. struct pwm_cdev_data {
  1735. struct pwm_chip *chip;
  1736. struct pwm_device *pwm[];
  1737. };
  1738. static int pwm_cdev_open(struct inode *inode, struct file *file)
  1739. {
  1740. struct pwm_chip *chip = container_of(inode->i_cdev, struct pwm_chip, cdev);
  1741. struct pwm_cdev_data *cdata;
  1742. guard(mutex)(&pwm_lock);
  1743. if (!chip->operational)
  1744. return -ENXIO;
  1745. cdata = kzalloc_flex(*cdata, pwm, chip->npwm);
  1746. if (!cdata)
  1747. return -ENOMEM;
  1748. cdata->chip = chip;
  1749. file->private_data = cdata;
  1750. return nonseekable_open(inode, file);
  1751. }
  1752. static int pwm_cdev_release(struct inode *inode, struct file *file)
  1753. {
  1754. struct pwm_cdev_data *cdata = file->private_data;
  1755. unsigned int i;
  1756. for (i = 0; i < cdata->chip->npwm; ++i) {
  1757. struct pwm_device *pwm = cdata->pwm[i];
  1758. if (pwm) {
  1759. const char *label = pwm->label;
  1760. pwm_put(cdata->pwm[i]);
  1761. kfree(label);
  1762. }
  1763. }
  1764. kfree(cdata);
  1765. return 0;
  1766. }
  1767. static int pwm_cdev_request(struct pwm_cdev_data *cdata, unsigned int hwpwm)
  1768. {
  1769. struct pwm_chip *chip = cdata->chip;
  1770. if (hwpwm >= chip->npwm)
  1771. return -EINVAL;
  1772. if (!cdata->pwm[hwpwm]) {
  1773. struct pwm_device *pwm = &chip->pwms[hwpwm];
  1774. const char *label;
  1775. int ret;
  1776. label = kasprintf(GFP_KERNEL, "pwm-cdev (pid=%d)", current->pid);
  1777. if (!label)
  1778. return -ENOMEM;
  1779. ret = pwm_device_request(pwm, label);
  1780. if (ret < 0) {
  1781. kfree(label);
  1782. return ret;
  1783. }
  1784. cdata->pwm[hwpwm] = pwm;
  1785. }
  1786. return 0;
  1787. }
  1788. static int pwm_cdev_free(struct pwm_cdev_data *cdata, unsigned int hwpwm)
  1789. {
  1790. struct pwm_chip *chip = cdata->chip;
  1791. if (hwpwm >= chip->npwm)
  1792. return -EINVAL;
  1793. if (cdata->pwm[hwpwm]) {
  1794. struct pwm_device *pwm = cdata->pwm[hwpwm];
  1795. const char *label = pwm->label;
  1796. __pwm_put(pwm);
  1797. kfree(label);
  1798. cdata->pwm[hwpwm] = NULL;
  1799. }
  1800. return 0;
  1801. }
  1802. static struct pwm_device *pwm_cdev_get_requested_pwm(struct pwm_cdev_data *cdata,
  1803. u32 hwpwm)
  1804. {
  1805. struct pwm_chip *chip = cdata->chip;
  1806. if (hwpwm >= chip->npwm)
  1807. return ERR_PTR(-EINVAL);
  1808. if (cdata->pwm[hwpwm])
  1809. return cdata->pwm[hwpwm];
  1810. return ERR_PTR(-EINVAL);
  1811. }
  1812. static long pwm_cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1813. {
  1814. int ret = 0;
  1815. struct pwm_cdev_data *cdata = file->private_data;
  1816. struct pwm_chip *chip = cdata->chip;
  1817. guard(mutex)(&pwm_lock);
  1818. if (!chip->operational)
  1819. return -ENODEV;
  1820. switch (cmd) {
  1821. case PWM_IOCTL_REQUEST:
  1822. {
  1823. unsigned int hwpwm = arg;
  1824. return pwm_cdev_request(cdata, hwpwm);
  1825. }
  1826. case PWM_IOCTL_FREE:
  1827. {
  1828. unsigned int hwpwm = arg;
  1829. return pwm_cdev_free(cdata, hwpwm);
  1830. }
  1831. case PWM_IOCTL_ROUNDWF:
  1832. {
  1833. struct pwmchip_waveform cwf;
  1834. struct pwm_waveform wf;
  1835. struct pwm_device *pwm;
  1836. ret = copy_from_user(&cwf,
  1837. (struct pwmchip_waveform __user *)arg,
  1838. sizeof(cwf));
  1839. if (ret)
  1840. return -EFAULT;
  1841. if (cwf.__pad != 0)
  1842. return -EINVAL;
  1843. pwm = pwm_cdev_get_requested_pwm(cdata, cwf.hwpwm);
  1844. if (IS_ERR(pwm))
  1845. return PTR_ERR(pwm);
  1846. wf = (struct pwm_waveform) {
  1847. .period_length_ns = cwf.period_length_ns,
  1848. .duty_length_ns = cwf.duty_length_ns,
  1849. .duty_offset_ns = cwf.duty_offset_ns,
  1850. };
  1851. ret = pwm_round_waveform_might_sleep(pwm, &wf);
  1852. if (ret < 0)
  1853. return ret;
  1854. cwf = (struct pwmchip_waveform) {
  1855. .hwpwm = cwf.hwpwm,
  1856. .period_length_ns = wf.period_length_ns,
  1857. .duty_length_ns = wf.duty_length_ns,
  1858. .duty_offset_ns = wf.duty_offset_ns,
  1859. };
  1860. ret = copy_to_user((struct pwmchip_waveform __user *)arg,
  1861. &cwf, sizeof(cwf));
  1862. return ret ? -EFAULT : 0;
  1863. }
  1864. case PWM_IOCTL_GETWF:
  1865. {
  1866. struct pwmchip_waveform cwf;
  1867. struct pwm_waveform wf;
  1868. struct pwm_device *pwm;
  1869. ret = copy_from_user(&cwf,
  1870. (struct pwmchip_waveform __user *)arg,
  1871. sizeof(cwf));
  1872. if (ret)
  1873. return -EFAULT;
  1874. if (cwf.__pad != 0)
  1875. return -EINVAL;
  1876. pwm = pwm_cdev_get_requested_pwm(cdata, cwf.hwpwm);
  1877. if (IS_ERR(pwm))
  1878. return PTR_ERR(pwm);
  1879. ret = pwm_get_waveform_might_sleep(pwm, &wf);
  1880. if (ret)
  1881. return ret;
  1882. cwf = (struct pwmchip_waveform) {
  1883. .hwpwm = cwf.hwpwm,
  1884. .period_length_ns = wf.period_length_ns,
  1885. .duty_length_ns = wf.duty_length_ns,
  1886. .duty_offset_ns = wf.duty_offset_ns,
  1887. };
  1888. ret = copy_to_user((struct pwmchip_waveform __user *)arg,
  1889. &cwf, sizeof(cwf));
  1890. return ret ? -EFAULT : 0;
  1891. }
  1892. case PWM_IOCTL_SETROUNDEDWF:
  1893. case PWM_IOCTL_SETEXACTWF:
  1894. {
  1895. struct pwmchip_waveform cwf;
  1896. struct pwm_waveform wf;
  1897. struct pwm_device *pwm;
  1898. ret = copy_from_user(&cwf,
  1899. (struct pwmchip_waveform __user *)arg,
  1900. sizeof(cwf));
  1901. if (ret)
  1902. return -EFAULT;
  1903. if (cwf.__pad != 0)
  1904. return -EINVAL;
  1905. wf = (struct pwm_waveform){
  1906. .period_length_ns = cwf.period_length_ns,
  1907. .duty_length_ns = cwf.duty_length_ns,
  1908. .duty_offset_ns = cwf.duty_offset_ns,
  1909. };
  1910. if (!pwm_wf_valid(&wf))
  1911. return -EINVAL;
  1912. pwm = pwm_cdev_get_requested_pwm(cdata, cwf.hwpwm);
  1913. if (IS_ERR(pwm))
  1914. return PTR_ERR(pwm);
  1915. ret = pwm_set_waveform_might_sleep(pwm, &wf,
  1916. cmd == PWM_IOCTL_SETEXACTWF);
  1917. /*
  1918. * If userspace cares about rounding deviations it has
  1919. * to check the values anyhow, so simplify handling for
  1920. * them and don't signal uprounding. This matches the
  1921. * behaviour of PWM_IOCTL_ROUNDWF which also returns 0
  1922. * in that case.
  1923. */
  1924. if (ret == 1)
  1925. ret = 0;
  1926. return ret;
  1927. }
  1928. default:
  1929. return -ENOTTY;
  1930. }
  1931. }
  1932. static const struct file_operations pwm_cdev_fileops = {
  1933. .open = pwm_cdev_open,
  1934. .release = pwm_cdev_release,
  1935. .owner = THIS_MODULE,
  1936. .unlocked_ioctl = pwm_cdev_ioctl,
  1937. };
  1938. static dev_t pwm_devt;
  1939. static int pwm_gpio_request(struct gpio_chip *gc, unsigned int offset)
  1940. {
  1941. struct pwm_chip *chip = gpiochip_get_data(gc);
  1942. struct pwm_device *pwm;
  1943. pwm = pwm_request_from_chip(chip, offset, "pwm-gpio");
  1944. if (IS_ERR(pwm))
  1945. return PTR_ERR(pwm);
  1946. return 0;
  1947. }
  1948. static void pwm_gpio_free(struct gpio_chip *gc, unsigned int offset)
  1949. {
  1950. struct pwm_chip *chip = gpiochip_get_data(gc);
  1951. pwm_put(&chip->pwms[offset]);
  1952. }
  1953. static int pwm_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  1954. {
  1955. return GPIO_LINE_DIRECTION_OUT;
  1956. }
  1957. static int pwm_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
  1958. {
  1959. struct pwm_chip *chip = gpiochip_get_data(gc);
  1960. struct pwm_device *pwm = &chip->pwms[offset];
  1961. int ret;
  1962. struct pwm_waveform wf = {
  1963. .period_length_ns = 1,
  1964. };
  1965. ret = pwm_round_waveform_might_sleep(pwm, &wf);
  1966. if (ret < 0)
  1967. return ret;
  1968. if (value)
  1969. wf.duty_length_ns = wf.period_length_ns;
  1970. else
  1971. wf.duty_length_ns = 0;
  1972. return pwm_set_waveform_might_sleep(pwm, &wf, true);
  1973. }
  1974. /**
  1975. * __pwmchip_add() - register a new PWM chip
  1976. * @chip: the PWM chip to add
  1977. * @owner: reference to the module providing the chip.
  1978. *
  1979. * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
  1980. * pwmchip_add wrapper to do this right.
  1981. *
  1982. * Returns: 0 on success or a negative error code on failure.
  1983. */
  1984. int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
  1985. {
  1986. int ret;
  1987. if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
  1988. return -EINVAL;
  1989. /*
  1990. * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
  1991. * otherwise the embedded struct device might disappear too early
  1992. * resulting in memory corruption.
  1993. * Catch drivers that were not converted appropriately.
  1994. */
  1995. if (!chip->uses_pwmchip_alloc)
  1996. return -EINVAL;
  1997. if (!pwm_ops_check(chip))
  1998. return -EINVAL;
  1999. chip->owner = owner;
  2000. if (chip->atomic)
  2001. spin_lock_init(&chip->atomic_lock);
  2002. else
  2003. mutex_init(&chip->nonatomic_lock);
  2004. guard(mutex)(&pwm_lock);
  2005. ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
  2006. if (ret < 0)
  2007. return ret;
  2008. chip->id = ret;
  2009. dev_set_name(&chip->dev, "pwmchip%u", chip->id);
  2010. if (IS_ENABLED(CONFIG_OF))
  2011. of_pwmchip_add(chip);
  2012. scoped_guard(pwmchip, chip)
  2013. chip->operational = true;
  2014. if (chip->ops->write_waveform) {
  2015. if (chip->id < PWM_MINOR_COUNT)
  2016. chip->dev.devt = MKDEV(MAJOR(pwm_devt), chip->id);
  2017. else
  2018. dev_warn(&chip->dev, "chip id too high to create a chardev\n");
  2019. }
  2020. cdev_init(&chip->cdev, &pwm_cdev_fileops);
  2021. chip->cdev.owner = owner;
  2022. ret = cdev_device_add(&chip->cdev, &chip->dev);
  2023. if (ret)
  2024. goto err_device_add;
  2025. if (IS_ENABLED(CONFIG_PWM_PROVIDE_GPIO) && chip->ops->write_waveform) {
  2026. struct device *parent = pwmchip_parent(chip);
  2027. chip->gpio = (typeof(chip->gpio)){
  2028. .label = dev_name(parent),
  2029. .parent = parent,
  2030. .request = pwm_gpio_request,
  2031. .free = pwm_gpio_free,
  2032. .get_direction = pwm_gpio_get_direction,
  2033. .set = pwm_gpio_set,
  2034. .base = -1,
  2035. .ngpio = chip->npwm,
  2036. .can_sleep = true,
  2037. };
  2038. ret = gpiochip_add_data(&chip->gpio, chip);
  2039. if (ret)
  2040. goto err_gpiochip_add;
  2041. }
  2042. return 0;
  2043. err_gpiochip_add:
  2044. cdev_device_del(&chip->cdev, &chip->dev);
  2045. err_device_add:
  2046. scoped_guard(pwmchip, chip)
  2047. chip->operational = false;
  2048. if (IS_ENABLED(CONFIG_OF))
  2049. of_pwmchip_remove(chip);
  2050. idr_remove(&pwm_chips, chip->id);
  2051. return ret;
  2052. }
  2053. EXPORT_SYMBOL_GPL(__pwmchip_add);
  2054. /**
  2055. * pwmchip_remove() - remove a PWM chip
  2056. * @chip: the PWM chip to remove
  2057. *
  2058. * Removes a PWM chip.
  2059. */
  2060. void pwmchip_remove(struct pwm_chip *chip)
  2061. {
  2062. if (IS_ENABLED(CONFIG_PWM_PROVIDE_GPIO) && chip->ops->write_waveform)
  2063. gpiochip_remove(&chip->gpio);
  2064. pwmchip_sysfs_unexport(chip);
  2065. scoped_guard(mutex, &pwm_lock) {
  2066. unsigned int i;
  2067. scoped_guard(pwmchip, chip)
  2068. chip->operational = false;
  2069. for (i = 0; i < chip->npwm; ++i) {
  2070. struct pwm_device *pwm = &chip->pwms[i];
  2071. if (test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  2072. dev_warn(&chip->dev, "Freeing requested PWM #%u\n", i);
  2073. if (pwm->chip->ops->free)
  2074. pwm->chip->ops->free(pwm->chip, pwm);
  2075. }
  2076. }
  2077. if (IS_ENABLED(CONFIG_OF))
  2078. of_pwmchip_remove(chip);
  2079. idr_remove(&pwm_chips, chip->id);
  2080. }
  2081. cdev_device_del(&chip->cdev, &chip->dev);
  2082. }
  2083. EXPORT_SYMBOL_GPL(pwmchip_remove);
  2084. static void devm_pwmchip_remove(void *data)
  2085. {
  2086. struct pwm_chip *chip = data;
  2087. pwmchip_remove(chip);
  2088. }
  2089. int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
  2090. {
  2091. int ret;
  2092. ret = __pwmchip_add(chip, owner);
  2093. if (ret)
  2094. return ret;
  2095. return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
  2096. }
  2097. EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
  2098. /**
  2099. * pwm_add_table() - register PWM device consumers
  2100. * @table: array of consumers to register
  2101. * @num: number of consumers in table
  2102. */
  2103. void pwm_add_table(struct pwm_lookup *table, size_t num)
  2104. {
  2105. guard(mutex)(&pwm_lookup_lock);
  2106. while (num--) {
  2107. list_add_tail(&table->list, &pwm_lookup_list);
  2108. table++;
  2109. }
  2110. }
  2111. /**
  2112. * pwm_remove_table() - unregister PWM device consumers
  2113. * @table: array of consumers to unregister
  2114. * @num: number of consumers in table
  2115. */
  2116. void pwm_remove_table(struct pwm_lookup *table, size_t num)
  2117. {
  2118. guard(mutex)(&pwm_lookup_lock);
  2119. while (num--) {
  2120. list_del(&table->list);
  2121. table++;
  2122. }
  2123. }
  2124. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  2125. {
  2126. unsigned int i;
  2127. for (i = 0; i < chip->npwm; i++) {
  2128. struct pwm_device *pwm = &chip->pwms[i];
  2129. struct pwm_state state;
  2130. int err;
  2131. pwm_get_state(pwm, &state);
  2132. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  2133. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  2134. seq_puts(s, " requested");
  2135. seq_puts(s, "\n");
  2136. seq_printf(s, " requested configuration: %3sabled, %llu/%llu ns, %s polarity",
  2137. state.enabled ? "en" : "dis", state.duty_cycle, state.period,
  2138. state.polarity ? "inverse" : "normal");
  2139. if (state.usage_power)
  2140. seq_puts(s, ", usage_power");
  2141. seq_puts(s, "\n");
  2142. if (pwmchip_supports_waveform(chip)) {
  2143. struct pwm_waveform wf;
  2144. err = pwm_get_waveform_might_sleep(pwm, &wf);
  2145. if (!err)
  2146. seq_printf(s, " actual configuration: %lld/%lld [+%lld]",
  2147. wf.duty_length_ns, wf.period_length_ns, wf.duty_offset_ns);
  2148. else
  2149. seq_printf(s, " actual configuration: read out error: %pe\n", ERR_PTR(err));
  2150. } else {
  2151. struct pwm_state hwstate;
  2152. err = pwm_get_state_hw(pwm, &hwstate);
  2153. if (!err)
  2154. seq_printf(s, " actual configuration: %3sabled, %llu/%llu ns, %s polarity",
  2155. hwstate.enabled ? "en" : "dis", hwstate.duty_cycle, hwstate.period,
  2156. hwstate.polarity ? "inverse" : "normal");
  2157. else
  2158. seq_printf(s, " actual configuration: read out error: %pe", ERR_PTR(err));
  2159. }
  2160. seq_puts(s, "\n");
  2161. }
  2162. }
  2163. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  2164. {
  2165. unsigned long id = *pos;
  2166. void *ret;
  2167. mutex_lock(&pwm_lock);
  2168. s->private = "";
  2169. ret = idr_get_next_ul(&pwm_chips, &id);
  2170. *pos = id;
  2171. return ret;
  2172. }
  2173. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2174. {
  2175. unsigned long id = *pos + 1;
  2176. void *ret;
  2177. s->private = "\n";
  2178. ret = idr_get_next_ul(&pwm_chips, &id);
  2179. *pos = id;
  2180. return ret;
  2181. }
  2182. static void pwm_seq_stop(struct seq_file *s, void *v)
  2183. {
  2184. mutex_unlock(&pwm_lock);
  2185. }
  2186. static int pwm_seq_show(struct seq_file *s, void *v)
  2187. {
  2188. struct pwm_chip *chip = v;
  2189. seq_printf(s, "%s%u: %s/%s, npwm: %u\n",
  2190. (char *)s->private, chip->id,
  2191. pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
  2192. dev_name(pwmchip_parent(chip)), chip->npwm);
  2193. pwm_dbg_show(chip, s);
  2194. return 0;
  2195. }
  2196. static const struct seq_operations pwm_debugfs_sops = {
  2197. .start = pwm_seq_start,
  2198. .next = pwm_seq_next,
  2199. .stop = pwm_seq_stop,
  2200. .show = pwm_seq_show,
  2201. };
  2202. DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
  2203. static int __init pwm_init(void)
  2204. {
  2205. int ret;
  2206. ret = alloc_chrdev_region(&pwm_devt, 0, PWM_MINOR_COUNT, "pwm");
  2207. if (ret) {
  2208. pr_err("Failed to initialize chrdev region for PWM usage\n");
  2209. return ret;
  2210. }
  2211. ret = class_register(&pwm_class);
  2212. if (ret) {
  2213. pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
  2214. unregister_chrdev_region(pwm_devt, 256);
  2215. return ret;
  2216. }
  2217. if (IS_ENABLED(CONFIG_DEBUG_FS))
  2218. debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
  2219. return 0;
  2220. }
  2221. subsys_initcall(pwm_init);