core.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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. #define CREATE_TRACE_POINTS
  23. #include <trace/events/pwm.h>
  24. /* protects access to pwm_chips */
  25. static DEFINE_MUTEX(pwm_lock);
  26. static DEFINE_IDR(pwm_chips);
  27. static void pwm_apply_debug(struct pwm_device *pwm,
  28. const struct pwm_state *state)
  29. {
  30. struct pwm_state *last = &pwm->last;
  31. struct pwm_chip *chip = pwm->chip;
  32. struct pwm_state s1 = { 0 }, s2 = { 0 };
  33. int err;
  34. if (!IS_ENABLED(CONFIG_PWM_DEBUG))
  35. return;
  36. /* No reasonable diagnosis possible without .get_state() */
  37. if (!chip->ops->get_state)
  38. return;
  39. /*
  40. * *state was just applied. Read out the hardware state and do some
  41. * checks.
  42. */
  43. err = chip->ops->get_state(chip, pwm, &s1);
  44. trace_pwm_get(pwm, &s1, err);
  45. if (err)
  46. /* If that failed there isn't much to debug */
  47. return;
  48. /*
  49. * The lowlevel driver either ignored .polarity (which is a bug) or as
  50. * best effort inverted .polarity and fixed .duty_cycle respectively.
  51. * Undo this inversion and fixup for further tests.
  52. */
  53. if (s1.enabled && s1.polarity != state->polarity) {
  54. s2.polarity = state->polarity;
  55. s2.duty_cycle = s1.period - s1.duty_cycle;
  56. s2.period = s1.period;
  57. s2.enabled = s1.enabled;
  58. } else {
  59. s2 = s1;
  60. }
  61. if (s2.polarity != state->polarity &&
  62. state->duty_cycle < state->period)
  63. dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n");
  64. if (state->enabled && s2.enabled &&
  65. last->polarity == state->polarity &&
  66. last->period > s2.period &&
  67. last->period <= state->period)
  68. dev_warn(pwmchip_parent(chip),
  69. ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n",
  70. state->period, s2.period, last->period);
  71. /*
  72. * Rounding period up is fine only if duty_cycle is 0 then, because a
  73. * flat line doesn't have a characteristic period.
  74. */
  75. if (state->enabled && s2.enabled && state->period < s2.period && s2.duty_cycle)
  76. dev_warn(pwmchip_parent(chip),
  77. ".apply is supposed to round down period (requested: %llu, applied: %llu)\n",
  78. state->period, s2.period);
  79. if (state->enabled &&
  80. last->polarity == state->polarity &&
  81. last->period == s2.period &&
  82. last->duty_cycle > s2.duty_cycle &&
  83. last->duty_cycle <= state->duty_cycle)
  84. dev_warn(pwmchip_parent(chip),
  85. ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n",
  86. state->duty_cycle, state->period,
  87. s2.duty_cycle, s2.period,
  88. last->duty_cycle, last->period);
  89. if (state->enabled && s2.enabled && state->duty_cycle < s2.duty_cycle)
  90. dev_warn(pwmchip_parent(chip),
  91. ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n",
  92. state->duty_cycle, state->period,
  93. s2.duty_cycle, s2.period);
  94. if (!state->enabled && s2.enabled && s2.duty_cycle > 0)
  95. dev_warn(pwmchip_parent(chip),
  96. "requested disabled, but yielded enabled with duty > 0\n");
  97. /* reapply the state that the driver reported being configured. */
  98. err = chip->ops->apply(chip, pwm, &s1);
  99. trace_pwm_apply(pwm, &s1, err);
  100. if (err) {
  101. *last = s1;
  102. dev_err(pwmchip_parent(chip), "failed to reapply current setting\n");
  103. return;
  104. }
  105. *last = (struct pwm_state){ 0 };
  106. err = chip->ops->get_state(chip, pwm, last);
  107. trace_pwm_get(pwm, last, err);
  108. if (err)
  109. return;
  110. /* reapplication of the current state should give an exact match */
  111. if (s1.enabled != last->enabled ||
  112. s1.polarity != last->polarity ||
  113. (s1.enabled && s1.period != last->period) ||
  114. (s1.enabled && s1.duty_cycle != last->duty_cycle)) {
  115. dev_err(pwmchip_parent(chip),
  116. ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n",
  117. s1.enabled, s1.polarity, s1.duty_cycle, s1.period,
  118. last->enabled, last->polarity, last->duty_cycle,
  119. last->period);
  120. }
  121. }
  122. static bool pwm_state_valid(const struct pwm_state *state)
  123. {
  124. /*
  125. * For a disabled state all other state description is irrelevant and
  126. * and supposed to be ignored. So also ignore any strange values and
  127. * consider the state ok.
  128. */
  129. if (!state->enabled)
  130. return true;
  131. if (!state->period)
  132. return false;
  133. if (state->duty_cycle > state->period)
  134. return false;
  135. return true;
  136. }
  137. /**
  138. * __pwm_apply() - atomically apply a new state to a PWM device
  139. * @pwm: PWM device
  140. * @state: new state to apply
  141. */
  142. static int __pwm_apply(struct pwm_device *pwm, const struct pwm_state *state)
  143. {
  144. struct pwm_chip *chip;
  145. int err;
  146. if (!pwm || !state)
  147. return -EINVAL;
  148. if (!pwm_state_valid(state)) {
  149. /*
  150. * Allow to transition from one invalid state to another.
  151. * This ensures that you can e.g. change the polarity while
  152. * the period is zero. (This happens on stm32 when the hardware
  153. * is in its poweron default state.) This greatly simplifies
  154. * working with the sysfs API where you can only change one
  155. * parameter at a time.
  156. */
  157. if (!pwm_state_valid(&pwm->state)) {
  158. pwm->state = *state;
  159. return 0;
  160. }
  161. return -EINVAL;
  162. }
  163. chip = pwm->chip;
  164. if (state->period == pwm->state.period &&
  165. state->duty_cycle == pwm->state.duty_cycle &&
  166. state->polarity == pwm->state.polarity &&
  167. state->enabled == pwm->state.enabled &&
  168. state->usage_power == pwm->state.usage_power)
  169. return 0;
  170. err = chip->ops->apply(chip, pwm, state);
  171. trace_pwm_apply(pwm, state, err);
  172. if (err)
  173. return err;
  174. pwm->state = *state;
  175. /*
  176. * only do this after pwm->state was applied as some
  177. * implementations of .get_state depend on this
  178. */
  179. pwm_apply_debug(pwm, state);
  180. return 0;
  181. }
  182. /**
  183. * pwm_apply_might_sleep() - atomically apply a new state to a PWM device
  184. * Cannot be used in atomic context.
  185. * @pwm: PWM device
  186. * @state: new state to apply
  187. */
  188. int pwm_apply_might_sleep(struct pwm_device *pwm, const struct pwm_state *state)
  189. {
  190. int err;
  191. /*
  192. * Some lowlevel driver's implementations of .apply() make use of
  193. * mutexes, also with some drivers only returning when the new
  194. * configuration is active calling pwm_apply_might_sleep() from atomic context
  195. * is a bad idea. So make it explicit that calling this function might
  196. * sleep.
  197. */
  198. might_sleep();
  199. if (IS_ENABLED(CONFIG_PWM_DEBUG) && pwm->chip->atomic) {
  200. /*
  201. * Catch any drivers that have been marked as atomic but
  202. * that will sleep anyway.
  203. */
  204. non_block_start();
  205. err = __pwm_apply(pwm, state);
  206. non_block_end();
  207. } else {
  208. err = __pwm_apply(pwm, state);
  209. }
  210. return err;
  211. }
  212. EXPORT_SYMBOL_GPL(pwm_apply_might_sleep);
  213. /**
  214. * pwm_apply_atomic() - apply a new state to a PWM device from atomic context
  215. * Not all PWM devices support this function, check with pwm_might_sleep().
  216. * @pwm: PWM device
  217. * @state: new state to apply
  218. */
  219. int pwm_apply_atomic(struct pwm_device *pwm, const struct pwm_state *state)
  220. {
  221. WARN_ONCE(!pwm->chip->atomic,
  222. "sleeping PWM driver used in atomic context\n");
  223. return __pwm_apply(pwm, state);
  224. }
  225. EXPORT_SYMBOL_GPL(pwm_apply_atomic);
  226. /**
  227. * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
  228. * @pwm: PWM device
  229. *
  230. * This function will adjust the PWM config to the PWM arguments provided
  231. * by the DT or PWM lookup table. This is particularly useful to adapt
  232. * the bootloader config to the Linux one.
  233. */
  234. int pwm_adjust_config(struct pwm_device *pwm)
  235. {
  236. struct pwm_state state;
  237. struct pwm_args pargs;
  238. pwm_get_args(pwm, &pargs);
  239. pwm_get_state(pwm, &state);
  240. /*
  241. * If the current period is zero it means that either the PWM driver
  242. * does not support initial state retrieval or the PWM has not yet
  243. * been configured.
  244. *
  245. * In either case, we setup the new period and polarity, and assign a
  246. * duty cycle of 0.
  247. */
  248. if (!state.period) {
  249. state.duty_cycle = 0;
  250. state.period = pargs.period;
  251. state.polarity = pargs.polarity;
  252. return pwm_apply_might_sleep(pwm, &state);
  253. }
  254. /*
  255. * Adjust the PWM duty cycle/period based on the period value provided
  256. * in PWM args.
  257. */
  258. if (pargs.period != state.period) {
  259. u64 dutycycle = (u64)state.duty_cycle * pargs.period;
  260. do_div(dutycycle, state.period);
  261. state.duty_cycle = dutycycle;
  262. state.period = pargs.period;
  263. }
  264. /*
  265. * If the polarity changed, we should also change the duty cycle.
  266. */
  267. if (pargs.polarity != state.polarity) {
  268. state.polarity = pargs.polarity;
  269. state.duty_cycle = state.period - state.duty_cycle;
  270. }
  271. return pwm_apply_might_sleep(pwm, &state);
  272. }
  273. EXPORT_SYMBOL_GPL(pwm_adjust_config);
  274. /**
  275. * pwm_capture() - capture and report a PWM signal
  276. * @pwm: PWM device
  277. * @result: structure to fill with capture result
  278. * @timeout: time to wait, in milliseconds, before giving up on capture
  279. *
  280. * Returns: 0 on success or a negative error code on failure.
  281. */
  282. static int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
  283. unsigned long timeout)
  284. {
  285. struct pwm_chip *chip = pwm->chip;
  286. const struct pwm_ops *ops = chip->ops;
  287. if (!ops->capture)
  288. return -ENOSYS;
  289. guard(mutex)(&pwm_lock);
  290. return ops->capture(chip, pwm, result, timeout);
  291. }
  292. static struct pwm_chip *pwmchip_find_by_name(const char *name)
  293. {
  294. struct pwm_chip *chip;
  295. unsigned long id, tmp;
  296. if (!name)
  297. return NULL;
  298. guard(mutex)(&pwm_lock);
  299. idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) {
  300. const char *chip_name = dev_name(pwmchip_parent(chip));
  301. if (chip_name && strcmp(chip_name, name) == 0)
  302. return chip;
  303. }
  304. return NULL;
  305. }
  306. static int pwm_device_request(struct pwm_device *pwm, const char *label)
  307. {
  308. int err;
  309. struct pwm_chip *chip = pwm->chip;
  310. const struct pwm_ops *ops = chip->ops;
  311. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  312. return -EBUSY;
  313. if (!try_module_get(chip->owner))
  314. return -ENODEV;
  315. if (!get_device(&chip->dev)) {
  316. err = -ENODEV;
  317. goto err_get_device;
  318. }
  319. if (ops->request) {
  320. err = ops->request(chip, pwm);
  321. if (err) {
  322. put_device(&chip->dev);
  323. err_get_device:
  324. module_put(chip->owner);
  325. return err;
  326. }
  327. }
  328. if (ops->get_state) {
  329. /*
  330. * Zero-initialize state because most drivers are unaware of
  331. * .usage_power. The other members of state are supposed to be
  332. * set by lowlevel drivers. We still initialize the whole
  333. * structure for simplicity even though this might paper over
  334. * faulty implementations of .get_state().
  335. */
  336. struct pwm_state state = { 0, };
  337. err = ops->get_state(chip, pwm, &state);
  338. trace_pwm_get(pwm, &state, err);
  339. if (!err)
  340. pwm->state = state;
  341. if (IS_ENABLED(CONFIG_PWM_DEBUG))
  342. pwm->last = pwm->state;
  343. }
  344. set_bit(PWMF_REQUESTED, &pwm->flags);
  345. pwm->label = label;
  346. return 0;
  347. }
  348. /**
  349. * pwm_request_from_chip() - request a PWM device relative to a PWM chip
  350. * @chip: PWM chip
  351. * @index: per-chip index of the PWM to request
  352. * @label: a literal description string of this PWM
  353. *
  354. * Returns: A pointer to the PWM device at the given index of the given PWM
  355. * chip. A negative error code is returned if the index is not valid for the
  356. * specified PWM chip or if the PWM device cannot be requested.
  357. */
  358. static struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
  359. unsigned int index,
  360. const char *label)
  361. {
  362. struct pwm_device *pwm;
  363. int err;
  364. if (!chip || index >= chip->npwm)
  365. return ERR_PTR(-EINVAL);
  366. guard(mutex)(&pwm_lock);
  367. pwm = &chip->pwms[index];
  368. err = pwm_device_request(pwm, label);
  369. if (err < 0)
  370. return ERR_PTR(err);
  371. return pwm;
  372. }
  373. struct pwm_device *
  374. of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args)
  375. {
  376. struct pwm_device *pwm;
  377. /* period in the second cell and flags in the third cell are optional */
  378. if (args->args_count < 1)
  379. return ERR_PTR(-EINVAL);
  380. pwm = pwm_request_from_chip(chip, args->args[0], NULL);
  381. if (IS_ERR(pwm))
  382. return pwm;
  383. if (args->args_count > 1)
  384. pwm->args.period = args->args[1];
  385. pwm->args.polarity = PWM_POLARITY_NORMAL;
  386. if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED)
  387. pwm->args.polarity = PWM_POLARITY_INVERSED;
  388. return pwm;
  389. }
  390. EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags);
  391. struct pwm_device *
  392. of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args)
  393. {
  394. struct pwm_device *pwm;
  395. pwm = pwm_request_from_chip(chip, 0, NULL);
  396. if (IS_ERR(pwm))
  397. return pwm;
  398. if (args->args_count > 0)
  399. pwm->args.period = args->args[0];
  400. pwm->args.polarity = PWM_POLARITY_NORMAL;
  401. if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED)
  402. pwm->args.polarity = PWM_POLARITY_INVERSED;
  403. return pwm;
  404. }
  405. EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
  406. struct pwm_export {
  407. struct device pwm_dev;
  408. struct pwm_device *pwm;
  409. struct mutex lock;
  410. struct pwm_state suspend;
  411. };
  412. static inline struct pwm_chip *pwmchip_from_dev(struct device *pwmchip_dev)
  413. {
  414. return container_of(pwmchip_dev, struct pwm_chip, dev);
  415. }
  416. static inline struct pwm_export *pwmexport_from_dev(struct device *pwm_dev)
  417. {
  418. return container_of(pwm_dev, struct pwm_export, pwm_dev);
  419. }
  420. static inline struct pwm_device *pwm_from_dev(struct device *pwm_dev)
  421. {
  422. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  423. return export->pwm;
  424. }
  425. static ssize_t period_show(struct device *pwm_dev,
  426. struct device_attribute *attr,
  427. char *buf)
  428. {
  429. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  430. struct pwm_state state;
  431. pwm_get_state(pwm, &state);
  432. return sysfs_emit(buf, "%llu\n", state.period);
  433. }
  434. static ssize_t period_store(struct device *pwm_dev,
  435. struct device_attribute *attr,
  436. const char *buf, size_t size)
  437. {
  438. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  439. struct pwm_device *pwm = export->pwm;
  440. struct pwm_state state;
  441. u64 val;
  442. int ret;
  443. ret = kstrtou64(buf, 0, &val);
  444. if (ret)
  445. return ret;
  446. guard(mutex)(&export->lock);
  447. pwm_get_state(pwm, &state);
  448. state.period = val;
  449. ret = pwm_apply_might_sleep(pwm, &state);
  450. return ret ? : size;
  451. }
  452. static ssize_t duty_cycle_show(struct device *pwm_dev,
  453. struct device_attribute *attr,
  454. char *buf)
  455. {
  456. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  457. struct pwm_state state;
  458. pwm_get_state(pwm, &state);
  459. return sysfs_emit(buf, "%llu\n", state.duty_cycle);
  460. }
  461. static ssize_t duty_cycle_store(struct device *pwm_dev,
  462. struct device_attribute *attr,
  463. const char *buf, size_t size)
  464. {
  465. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  466. struct pwm_device *pwm = export->pwm;
  467. struct pwm_state state;
  468. u64 val;
  469. int ret;
  470. ret = kstrtou64(buf, 0, &val);
  471. if (ret)
  472. return ret;
  473. guard(mutex)(&export->lock);
  474. pwm_get_state(pwm, &state);
  475. state.duty_cycle = val;
  476. ret = pwm_apply_might_sleep(pwm, &state);
  477. return ret ? : size;
  478. }
  479. static ssize_t enable_show(struct device *pwm_dev,
  480. struct device_attribute *attr,
  481. char *buf)
  482. {
  483. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  484. struct pwm_state state;
  485. pwm_get_state(pwm, &state);
  486. return sysfs_emit(buf, "%d\n", state.enabled);
  487. }
  488. static ssize_t enable_store(struct device *pwm_dev,
  489. struct device_attribute *attr,
  490. const char *buf, size_t size)
  491. {
  492. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  493. struct pwm_device *pwm = export->pwm;
  494. struct pwm_state state;
  495. int val, ret;
  496. ret = kstrtoint(buf, 0, &val);
  497. if (ret)
  498. return ret;
  499. guard(mutex)(&export->lock);
  500. pwm_get_state(pwm, &state);
  501. switch (val) {
  502. case 0:
  503. state.enabled = false;
  504. break;
  505. case 1:
  506. state.enabled = true;
  507. break;
  508. default:
  509. return -EINVAL;
  510. }
  511. ret = pwm_apply_might_sleep(pwm, &state);
  512. return ret ? : size;
  513. }
  514. static ssize_t polarity_show(struct device *pwm_dev,
  515. struct device_attribute *attr,
  516. char *buf)
  517. {
  518. const struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  519. const char *polarity = "unknown";
  520. struct pwm_state state;
  521. pwm_get_state(pwm, &state);
  522. switch (state.polarity) {
  523. case PWM_POLARITY_NORMAL:
  524. polarity = "normal";
  525. break;
  526. case PWM_POLARITY_INVERSED:
  527. polarity = "inversed";
  528. break;
  529. }
  530. return sysfs_emit(buf, "%s\n", polarity);
  531. }
  532. static ssize_t polarity_store(struct device *pwm_dev,
  533. struct device_attribute *attr,
  534. const char *buf, size_t size)
  535. {
  536. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  537. struct pwm_device *pwm = export->pwm;
  538. enum pwm_polarity polarity;
  539. struct pwm_state state;
  540. int ret;
  541. if (sysfs_streq(buf, "normal"))
  542. polarity = PWM_POLARITY_NORMAL;
  543. else if (sysfs_streq(buf, "inversed"))
  544. polarity = PWM_POLARITY_INVERSED;
  545. else
  546. return -EINVAL;
  547. guard(mutex)(&export->lock);
  548. pwm_get_state(pwm, &state);
  549. state.polarity = polarity;
  550. ret = pwm_apply_might_sleep(pwm, &state);
  551. return ret ? : size;
  552. }
  553. static ssize_t capture_show(struct device *pwm_dev,
  554. struct device_attribute *attr,
  555. char *buf)
  556. {
  557. struct pwm_device *pwm = pwm_from_dev(pwm_dev);
  558. struct pwm_capture result;
  559. int ret;
  560. ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
  561. if (ret)
  562. return ret;
  563. return sysfs_emit(buf, "%u %u\n", result.period, result.duty_cycle);
  564. }
  565. static DEVICE_ATTR_RW(period);
  566. static DEVICE_ATTR_RW(duty_cycle);
  567. static DEVICE_ATTR_RW(enable);
  568. static DEVICE_ATTR_RW(polarity);
  569. static DEVICE_ATTR_RO(capture);
  570. static struct attribute *pwm_attrs[] = {
  571. &dev_attr_period.attr,
  572. &dev_attr_duty_cycle.attr,
  573. &dev_attr_enable.attr,
  574. &dev_attr_polarity.attr,
  575. &dev_attr_capture.attr,
  576. NULL
  577. };
  578. ATTRIBUTE_GROUPS(pwm);
  579. static void pwm_export_release(struct device *pwm_dev)
  580. {
  581. struct pwm_export *export = pwmexport_from_dev(pwm_dev);
  582. kfree(export);
  583. }
  584. static int pwm_export_child(struct device *pwmchip_dev, struct pwm_device *pwm)
  585. {
  586. struct pwm_export *export;
  587. char *pwm_prop[2];
  588. int ret;
  589. if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
  590. return -EBUSY;
  591. export = kzalloc(sizeof(*export), GFP_KERNEL);
  592. if (!export) {
  593. clear_bit(PWMF_EXPORTED, &pwm->flags);
  594. return -ENOMEM;
  595. }
  596. export->pwm = pwm;
  597. mutex_init(&export->lock);
  598. export->pwm_dev.release = pwm_export_release;
  599. export->pwm_dev.parent = pwmchip_dev;
  600. export->pwm_dev.devt = MKDEV(0, 0);
  601. export->pwm_dev.groups = pwm_groups;
  602. dev_set_name(&export->pwm_dev, "pwm%u", pwm->hwpwm);
  603. ret = device_register(&export->pwm_dev);
  604. if (ret) {
  605. clear_bit(PWMF_EXPORTED, &pwm->flags);
  606. put_device(&export->pwm_dev);
  607. export = NULL;
  608. return ret;
  609. }
  610. pwm_prop[0] = kasprintf(GFP_KERNEL, "EXPORT=pwm%u", pwm->hwpwm);
  611. pwm_prop[1] = NULL;
  612. kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
  613. kfree(pwm_prop[0]);
  614. return 0;
  615. }
  616. static int pwm_unexport_match(struct device *pwm_dev, void *data)
  617. {
  618. return pwm_from_dev(pwm_dev) == data;
  619. }
  620. static int pwm_unexport_child(struct device *pwmchip_dev, struct pwm_device *pwm)
  621. {
  622. struct device *pwm_dev;
  623. char *pwm_prop[2];
  624. if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
  625. return -ENODEV;
  626. pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
  627. if (!pwm_dev)
  628. return -ENODEV;
  629. pwm_prop[0] = kasprintf(GFP_KERNEL, "UNEXPORT=pwm%u", pwm->hwpwm);
  630. pwm_prop[1] = NULL;
  631. kobject_uevent_env(&pwmchip_dev->kobj, KOBJ_CHANGE, pwm_prop);
  632. kfree(pwm_prop[0]);
  633. /* for device_find_child() */
  634. put_device(pwm_dev);
  635. device_unregister(pwm_dev);
  636. pwm_put(pwm);
  637. return 0;
  638. }
  639. static ssize_t export_store(struct device *pwmchip_dev,
  640. struct device_attribute *attr,
  641. const char *buf, size_t len)
  642. {
  643. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  644. struct pwm_device *pwm;
  645. unsigned int hwpwm;
  646. int ret;
  647. ret = kstrtouint(buf, 0, &hwpwm);
  648. if (ret < 0)
  649. return ret;
  650. if (hwpwm >= chip->npwm)
  651. return -ENODEV;
  652. pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
  653. if (IS_ERR(pwm))
  654. return PTR_ERR(pwm);
  655. ret = pwm_export_child(pwmchip_dev, pwm);
  656. if (ret < 0)
  657. pwm_put(pwm);
  658. return ret ? : len;
  659. }
  660. static DEVICE_ATTR_WO(export);
  661. static ssize_t unexport_store(struct device *pwmchip_dev,
  662. struct device_attribute *attr,
  663. const char *buf, size_t len)
  664. {
  665. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  666. unsigned int hwpwm;
  667. int ret;
  668. ret = kstrtouint(buf, 0, &hwpwm);
  669. if (ret < 0)
  670. return ret;
  671. if (hwpwm >= chip->npwm)
  672. return -ENODEV;
  673. ret = pwm_unexport_child(pwmchip_dev, &chip->pwms[hwpwm]);
  674. return ret ? : len;
  675. }
  676. static DEVICE_ATTR_WO(unexport);
  677. static ssize_t npwm_show(struct device *pwmchip_dev, struct device_attribute *attr,
  678. char *buf)
  679. {
  680. const struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  681. return sysfs_emit(buf, "%u\n", chip->npwm);
  682. }
  683. static DEVICE_ATTR_RO(npwm);
  684. static struct attribute *pwm_chip_attrs[] = {
  685. &dev_attr_export.attr,
  686. &dev_attr_unexport.attr,
  687. &dev_attr_npwm.attr,
  688. NULL,
  689. };
  690. ATTRIBUTE_GROUPS(pwm_chip);
  691. /* takes export->lock on success */
  692. static struct pwm_export *pwm_class_get_state(struct device *pwmchip_dev,
  693. struct pwm_device *pwm,
  694. struct pwm_state *state)
  695. {
  696. struct device *pwm_dev;
  697. struct pwm_export *export;
  698. if (!test_bit(PWMF_EXPORTED, &pwm->flags))
  699. return NULL;
  700. pwm_dev = device_find_child(pwmchip_dev, pwm, pwm_unexport_match);
  701. if (!pwm_dev)
  702. return NULL;
  703. export = pwmexport_from_dev(pwm_dev);
  704. put_device(pwm_dev); /* for device_find_child() */
  705. mutex_lock(&export->lock);
  706. pwm_get_state(pwm, state);
  707. return export;
  708. }
  709. static int pwm_class_apply_state(struct pwm_export *export,
  710. struct pwm_device *pwm,
  711. struct pwm_state *state)
  712. {
  713. int ret = pwm_apply_might_sleep(pwm, state);
  714. /* release lock taken in pwm_class_get_state */
  715. mutex_unlock(&export->lock);
  716. return ret;
  717. }
  718. static int pwm_class_resume_npwm(struct device *pwmchip_dev, unsigned int npwm)
  719. {
  720. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  721. unsigned int i;
  722. int ret = 0;
  723. for (i = 0; i < npwm; i++) {
  724. struct pwm_device *pwm = &chip->pwms[i];
  725. struct pwm_state state;
  726. struct pwm_export *export;
  727. export = pwm_class_get_state(pwmchip_dev, pwm, &state);
  728. if (!export)
  729. continue;
  730. /* If pwmchip was not enabled before suspend, do nothing. */
  731. if (!export->suspend.enabled) {
  732. /* release lock taken in pwm_class_get_state */
  733. mutex_unlock(&export->lock);
  734. continue;
  735. }
  736. state.enabled = export->suspend.enabled;
  737. ret = pwm_class_apply_state(export, pwm, &state);
  738. if (ret < 0)
  739. break;
  740. }
  741. return ret;
  742. }
  743. static int pwm_class_suspend(struct device *pwmchip_dev)
  744. {
  745. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  746. unsigned int i;
  747. int ret = 0;
  748. for (i = 0; i < chip->npwm; i++) {
  749. struct pwm_device *pwm = &chip->pwms[i];
  750. struct pwm_state state;
  751. struct pwm_export *export;
  752. export = pwm_class_get_state(pwmchip_dev, pwm, &state);
  753. if (!export)
  754. continue;
  755. /*
  756. * If pwmchip was not enabled before suspend, save
  757. * state for resume time and do nothing else.
  758. */
  759. export->suspend = state;
  760. if (!state.enabled) {
  761. /* release lock taken in pwm_class_get_state */
  762. mutex_unlock(&export->lock);
  763. continue;
  764. }
  765. state.enabled = false;
  766. ret = pwm_class_apply_state(export, pwm, &state);
  767. if (ret < 0) {
  768. /*
  769. * roll back the PWM devices that were disabled by
  770. * this suspend function.
  771. */
  772. pwm_class_resume_npwm(pwmchip_dev, i);
  773. break;
  774. }
  775. }
  776. return ret;
  777. }
  778. static int pwm_class_resume(struct device *pwmchip_dev)
  779. {
  780. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  781. return pwm_class_resume_npwm(pwmchip_dev, chip->npwm);
  782. }
  783. static DEFINE_SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume);
  784. static struct class pwm_class = {
  785. .name = "pwm",
  786. .dev_groups = pwm_chip_groups,
  787. .pm = pm_sleep_ptr(&pwm_class_pm_ops),
  788. };
  789. static void pwmchip_sysfs_unexport(struct pwm_chip *chip)
  790. {
  791. unsigned int i;
  792. for (i = 0; i < chip->npwm; i++) {
  793. struct pwm_device *pwm = &chip->pwms[i];
  794. if (test_bit(PWMF_EXPORTED, &pwm->flags))
  795. pwm_unexport_child(&chip->dev, pwm);
  796. }
  797. }
  798. #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN
  799. static void *pwmchip_priv(struct pwm_chip *chip)
  800. {
  801. return (void *)chip + ALIGN(struct_size(chip, pwms, chip->npwm), PWMCHIP_ALIGN);
  802. }
  803. /* This is the counterpart to pwmchip_alloc() */
  804. void pwmchip_put(struct pwm_chip *chip)
  805. {
  806. put_device(&chip->dev);
  807. }
  808. EXPORT_SYMBOL_GPL(pwmchip_put);
  809. static void pwmchip_release(struct device *pwmchip_dev)
  810. {
  811. struct pwm_chip *chip = pwmchip_from_dev(pwmchip_dev);
  812. kfree(chip);
  813. }
  814. struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
  815. {
  816. struct pwm_chip *chip;
  817. struct device *pwmchip_dev;
  818. size_t alloc_size;
  819. unsigned int i;
  820. alloc_size = size_add(ALIGN(struct_size(chip, pwms, npwm), PWMCHIP_ALIGN),
  821. sizeof_priv);
  822. chip = kzalloc(alloc_size, GFP_KERNEL);
  823. if (!chip)
  824. return ERR_PTR(-ENOMEM);
  825. chip->npwm = npwm;
  826. chip->uses_pwmchip_alloc = true;
  827. pwmchip_dev = &chip->dev;
  828. device_initialize(pwmchip_dev);
  829. pwmchip_dev->class = &pwm_class;
  830. pwmchip_dev->parent = parent;
  831. pwmchip_dev->release = pwmchip_release;
  832. pwmchip_set_drvdata(chip, pwmchip_priv(chip));
  833. for (i = 0; i < chip->npwm; i++) {
  834. struct pwm_device *pwm = &chip->pwms[i];
  835. pwm->chip = chip;
  836. pwm->hwpwm = i;
  837. }
  838. return chip;
  839. }
  840. EXPORT_SYMBOL_GPL(pwmchip_alloc);
  841. static void devm_pwmchip_put(void *data)
  842. {
  843. struct pwm_chip *chip = data;
  844. pwmchip_put(chip);
  845. }
  846. struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
  847. {
  848. struct pwm_chip *chip;
  849. int ret;
  850. chip = pwmchip_alloc(parent, npwm, sizeof_priv);
  851. if (IS_ERR(chip))
  852. return chip;
  853. ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip);
  854. if (ret)
  855. return ERR_PTR(ret);
  856. return chip;
  857. }
  858. EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
  859. static void of_pwmchip_add(struct pwm_chip *chip)
  860. {
  861. if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node)
  862. return;
  863. if (!chip->of_xlate)
  864. chip->of_xlate = of_pwm_xlate_with_flags;
  865. of_node_get(pwmchip_parent(chip)->of_node);
  866. }
  867. static void of_pwmchip_remove(struct pwm_chip *chip)
  868. {
  869. if (pwmchip_parent(chip))
  870. of_node_put(pwmchip_parent(chip)->of_node);
  871. }
  872. static bool pwm_ops_check(const struct pwm_chip *chip)
  873. {
  874. const struct pwm_ops *ops = chip->ops;
  875. if (!ops->apply)
  876. return false;
  877. if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state)
  878. dev_warn(pwmchip_parent(chip),
  879. "Please implement the .get_state() callback\n");
  880. return true;
  881. }
  882. /**
  883. * __pwmchip_add() - register a new PWM chip
  884. * @chip: the PWM chip to add
  885. * @owner: reference to the module providing the chip.
  886. *
  887. * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the
  888. * pwmchip_add wrapper to do this right.
  889. *
  890. * Returns: 0 on success or a negative error code on failure.
  891. */
  892. int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
  893. {
  894. int ret;
  895. if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
  896. return -EINVAL;
  897. /*
  898. * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
  899. * otherwise the embedded struct device might disappear too early
  900. * resulting in memory corruption.
  901. * Catch drivers that were not converted appropriately.
  902. */
  903. if (!chip->uses_pwmchip_alloc)
  904. return -EINVAL;
  905. if (!pwm_ops_check(chip))
  906. return -EINVAL;
  907. chip->owner = owner;
  908. guard(mutex)(&pwm_lock);
  909. ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
  910. if (ret < 0)
  911. return ret;
  912. chip->id = ret;
  913. dev_set_name(&chip->dev, "pwmchip%u", chip->id);
  914. if (IS_ENABLED(CONFIG_OF))
  915. of_pwmchip_add(chip);
  916. ret = device_add(&chip->dev);
  917. if (ret)
  918. goto err_device_add;
  919. return 0;
  920. err_device_add:
  921. if (IS_ENABLED(CONFIG_OF))
  922. of_pwmchip_remove(chip);
  923. idr_remove(&pwm_chips, chip->id);
  924. return ret;
  925. }
  926. EXPORT_SYMBOL_GPL(__pwmchip_add);
  927. /**
  928. * pwmchip_remove() - remove a PWM chip
  929. * @chip: the PWM chip to remove
  930. *
  931. * Removes a PWM chip.
  932. */
  933. void pwmchip_remove(struct pwm_chip *chip)
  934. {
  935. pwmchip_sysfs_unexport(chip);
  936. if (IS_ENABLED(CONFIG_OF))
  937. of_pwmchip_remove(chip);
  938. scoped_guard(mutex, &pwm_lock)
  939. idr_remove(&pwm_chips, chip->id);
  940. device_del(&chip->dev);
  941. }
  942. EXPORT_SYMBOL_GPL(pwmchip_remove);
  943. static void devm_pwmchip_remove(void *data)
  944. {
  945. struct pwm_chip *chip = data;
  946. pwmchip_remove(chip);
  947. }
  948. int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner)
  949. {
  950. int ret;
  951. ret = __pwmchip_add(chip, owner);
  952. if (ret)
  953. return ret;
  954. return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip);
  955. }
  956. EXPORT_SYMBOL_GPL(__devm_pwmchip_add);
  957. static struct device_link *pwm_device_link_add(struct device *dev,
  958. struct pwm_device *pwm)
  959. {
  960. struct device_link *dl;
  961. if (!dev) {
  962. /*
  963. * No device for the PWM consumer has been provided. It may
  964. * impact the PM sequence ordering: the PWM supplier may get
  965. * suspended before the consumer.
  966. */
  967. dev_warn(pwmchip_parent(pwm->chip),
  968. "No consumer device specified to create a link to\n");
  969. return NULL;
  970. }
  971. dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER);
  972. if (!dl) {
  973. dev_err(dev, "failed to create device link to %s\n",
  974. dev_name(pwmchip_parent(pwm->chip)));
  975. return ERR_PTR(-EINVAL);
  976. }
  977. return dl;
  978. }
  979. static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode)
  980. {
  981. struct pwm_chip *chip;
  982. unsigned long id, tmp;
  983. guard(mutex)(&pwm_lock);
  984. idr_for_each_entry_ul(&pwm_chips, chip, tmp, id)
  985. if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode))
  986. return chip;
  987. return ERR_PTR(-EPROBE_DEFER);
  988. }
  989. /**
  990. * of_pwm_get() - request a PWM via the PWM framework
  991. * @dev: device for PWM consumer
  992. * @np: device node to get the PWM from
  993. * @con_id: consumer name
  994. *
  995. * Returns the PWM device parsed from the phandle and index specified in the
  996. * "pwms" property of a device tree node or a negative error-code on failure.
  997. * Values parsed from the device tree are stored in the returned PWM device
  998. * object.
  999. *
  1000. * If con_id is NULL, the first PWM device listed in the "pwms" property will
  1001. * be requested. Otherwise the "pwm-names" property is used to do a reverse
  1002. * lookup of the PWM index. This also means that the "pwm-names" property
  1003. * becomes mandatory for devices that look up the PWM device via the con_id
  1004. * parameter.
  1005. *
  1006. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1007. * error code on failure.
  1008. */
  1009. static struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
  1010. const char *con_id)
  1011. {
  1012. struct pwm_device *pwm = NULL;
  1013. struct of_phandle_args args;
  1014. struct device_link *dl;
  1015. struct pwm_chip *chip;
  1016. int index = 0;
  1017. int err;
  1018. if (con_id) {
  1019. index = of_property_match_string(np, "pwm-names", con_id);
  1020. if (index < 0)
  1021. return ERR_PTR(index);
  1022. }
  1023. err = of_parse_phandle_with_args(np, "pwms", "#pwm-cells", index,
  1024. &args);
  1025. if (err) {
  1026. pr_err("%s(): can't parse \"pwms\" property\n", __func__);
  1027. return ERR_PTR(err);
  1028. }
  1029. chip = fwnode_to_pwmchip(of_fwnode_handle(args.np));
  1030. if (IS_ERR(chip)) {
  1031. if (PTR_ERR(chip) != -EPROBE_DEFER)
  1032. pr_err("%s(): PWM chip not found\n", __func__);
  1033. pwm = ERR_CAST(chip);
  1034. goto put;
  1035. }
  1036. pwm = chip->of_xlate(chip, &args);
  1037. if (IS_ERR(pwm))
  1038. goto put;
  1039. dl = pwm_device_link_add(dev, pwm);
  1040. if (IS_ERR(dl)) {
  1041. /* of_xlate ended up calling pwm_request_from_chip() */
  1042. pwm_put(pwm);
  1043. pwm = ERR_CAST(dl);
  1044. goto put;
  1045. }
  1046. /*
  1047. * If a consumer name was not given, try to look it up from the
  1048. * "pwm-names" property if it exists. Otherwise use the name of
  1049. * the user device node.
  1050. */
  1051. if (!con_id) {
  1052. err = of_property_read_string_index(np, "pwm-names", index,
  1053. &con_id);
  1054. if (err < 0)
  1055. con_id = np->name;
  1056. }
  1057. pwm->label = con_id;
  1058. put:
  1059. of_node_put(args.np);
  1060. return pwm;
  1061. }
  1062. /**
  1063. * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI
  1064. * @fwnode: firmware node to get the "pwms" property from
  1065. *
  1066. * Returns the PWM device parsed from the fwnode and index specified in the
  1067. * "pwms" property or a negative error-code on failure.
  1068. * Values parsed from the device tree are stored in the returned PWM device
  1069. * object.
  1070. *
  1071. * This is analogous to of_pwm_get() except con_id is not yet supported.
  1072. * ACPI entries must look like
  1073. * Package () {"pwms", Package ()
  1074. * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}}
  1075. *
  1076. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1077. * error code on failure.
  1078. */
  1079. static struct pwm_device *acpi_pwm_get(const struct fwnode_handle *fwnode)
  1080. {
  1081. struct pwm_device *pwm;
  1082. struct fwnode_reference_args args;
  1083. struct pwm_chip *chip;
  1084. int ret;
  1085. memset(&args, 0, sizeof(args));
  1086. ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args);
  1087. if (ret < 0)
  1088. return ERR_PTR(ret);
  1089. if (args.nargs < 2)
  1090. return ERR_PTR(-EPROTO);
  1091. chip = fwnode_to_pwmchip(args.fwnode);
  1092. if (IS_ERR(chip))
  1093. return ERR_CAST(chip);
  1094. pwm = pwm_request_from_chip(chip, args.args[0], NULL);
  1095. if (IS_ERR(pwm))
  1096. return pwm;
  1097. pwm->args.period = args.args[1];
  1098. pwm->args.polarity = PWM_POLARITY_NORMAL;
  1099. if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED)
  1100. pwm->args.polarity = PWM_POLARITY_INVERSED;
  1101. return pwm;
  1102. }
  1103. static DEFINE_MUTEX(pwm_lookup_lock);
  1104. static LIST_HEAD(pwm_lookup_list);
  1105. /**
  1106. * pwm_add_table() - register PWM device consumers
  1107. * @table: array of consumers to register
  1108. * @num: number of consumers in table
  1109. */
  1110. void pwm_add_table(struct pwm_lookup *table, size_t num)
  1111. {
  1112. guard(mutex)(&pwm_lookup_lock);
  1113. while (num--) {
  1114. list_add_tail(&table->list, &pwm_lookup_list);
  1115. table++;
  1116. }
  1117. }
  1118. /**
  1119. * pwm_remove_table() - unregister PWM device consumers
  1120. * @table: array of consumers to unregister
  1121. * @num: number of consumers in table
  1122. */
  1123. void pwm_remove_table(struct pwm_lookup *table, size_t num)
  1124. {
  1125. guard(mutex)(&pwm_lookup_lock);
  1126. while (num--) {
  1127. list_del(&table->list);
  1128. table++;
  1129. }
  1130. }
  1131. /**
  1132. * pwm_get() - look up and request a PWM device
  1133. * @dev: device for PWM consumer
  1134. * @con_id: consumer name
  1135. *
  1136. * Lookup is first attempted using DT. If the device was not instantiated from
  1137. * a device tree, a PWM chip and a relative index is looked up via a table
  1138. * supplied by board setup code (see pwm_add_table()).
  1139. *
  1140. * Once a PWM chip has been found the specified PWM device will be requested
  1141. * and is ready to be used.
  1142. *
  1143. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1144. * error code on failure.
  1145. */
  1146. struct pwm_device *pwm_get(struct device *dev, const char *con_id)
  1147. {
  1148. const struct fwnode_handle *fwnode = dev ? dev_fwnode(dev) : NULL;
  1149. const char *dev_id = dev ? dev_name(dev) : NULL;
  1150. struct pwm_device *pwm;
  1151. struct pwm_chip *chip;
  1152. struct device_link *dl;
  1153. unsigned int best = 0;
  1154. struct pwm_lookup *p, *chosen = NULL;
  1155. unsigned int match;
  1156. int err;
  1157. /* look up via DT first */
  1158. if (is_of_node(fwnode))
  1159. return of_pwm_get(dev, to_of_node(fwnode), con_id);
  1160. /* then lookup via ACPI */
  1161. if (is_acpi_node(fwnode)) {
  1162. pwm = acpi_pwm_get(fwnode);
  1163. if (!IS_ERR(pwm) || PTR_ERR(pwm) != -ENOENT)
  1164. return pwm;
  1165. }
  1166. /*
  1167. * We look up the provider in the static table typically provided by
  1168. * board setup code. We first try to lookup the consumer device by
  1169. * name. If the consumer device was passed in as NULL or if no match
  1170. * was found, we try to find the consumer by directly looking it up
  1171. * by name.
  1172. *
  1173. * If a match is found, the provider PWM chip is looked up by name
  1174. * and a PWM device is requested using the PWM device per-chip index.
  1175. *
  1176. * The lookup algorithm was shamelessly taken from the clock
  1177. * framework:
  1178. *
  1179. * We do slightly fuzzy matching here:
  1180. * An entry with a NULL ID is assumed to be a wildcard.
  1181. * If an entry has a device ID, it must match
  1182. * If an entry has a connection ID, it must match
  1183. * Then we take the most specific entry - with the following order
  1184. * of precedence: dev+con > dev only > con only.
  1185. */
  1186. scoped_guard(mutex, &pwm_lookup_lock)
  1187. list_for_each_entry(p, &pwm_lookup_list, list) {
  1188. match = 0;
  1189. if (p->dev_id) {
  1190. if (!dev_id || strcmp(p->dev_id, dev_id))
  1191. continue;
  1192. match += 2;
  1193. }
  1194. if (p->con_id) {
  1195. if (!con_id || strcmp(p->con_id, con_id))
  1196. continue;
  1197. match += 1;
  1198. }
  1199. if (match > best) {
  1200. chosen = p;
  1201. if (match != 3)
  1202. best = match;
  1203. else
  1204. break;
  1205. }
  1206. }
  1207. if (!chosen)
  1208. return ERR_PTR(-ENODEV);
  1209. chip = pwmchip_find_by_name(chosen->provider);
  1210. /*
  1211. * If the lookup entry specifies a module, load the module and retry
  1212. * the PWM chip lookup. This can be used to work around driver load
  1213. * ordering issues if driver's can't be made to properly support the
  1214. * deferred probe mechanism.
  1215. */
  1216. if (!chip && chosen->module) {
  1217. err = request_module(chosen->module);
  1218. if (err == 0)
  1219. chip = pwmchip_find_by_name(chosen->provider);
  1220. }
  1221. if (!chip)
  1222. return ERR_PTR(-EPROBE_DEFER);
  1223. pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id);
  1224. if (IS_ERR(pwm))
  1225. return pwm;
  1226. dl = pwm_device_link_add(dev, pwm);
  1227. if (IS_ERR(dl)) {
  1228. pwm_put(pwm);
  1229. return ERR_CAST(dl);
  1230. }
  1231. pwm->args.period = chosen->period;
  1232. pwm->args.polarity = chosen->polarity;
  1233. return pwm;
  1234. }
  1235. EXPORT_SYMBOL_GPL(pwm_get);
  1236. /**
  1237. * pwm_put() - release a PWM device
  1238. * @pwm: PWM device
  1239. */
  1240. void pwm_put(struct pwm_device *pwm)
  1241. {
  1242. struct pwm_chip *chip;
  1243. if (!pwm)
  1244. return;
  1245. chip = pwm->chip;
  1246. guard(mutex)(&pwm_lock);
  1247. if (!test_and_clear_bit(PWMF_REQUESTED, &pwm->flags)) {
  1248. pr_warn("PWM device already freed\n");
  1249. return;
  1250. }
  1251. if (chip->ops->free)
  1252. pwm->chip->ops->free(pwm->chip, pwm);
  1253. pwm->label = NULL;
  1254. put_device(&chip->dev);
  1255. module_put(chip->owner);
  1256. }
  1257. EXPORT_SYMBOL_GPL(pwm_put);
  1258. static void devm_pwm_release(void *pwm)
  1259. {
  1260. pwm_put(pwm);
  1261. }
  1262. /**
  1263. * devm_pwm_get() - resource managed pwm_get()
  1264. * @dev: device for PWM consumer
  1265. * @con_id: consumer name
  1266. *
  1267. * This function performs like pwm_get() but the acquired PWM device will
  1268. * automatically be released on driver detach.
  1269. *
  1270. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1271. * error code on failure.
  1272. */
  1273. struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
  1274. {
  1275. struct pwm_device *pwm;
  1276. int ret;
  1277. pwm = pwm_get(dev, con_id);
  1278. if (IS_ERR(pwm))
  1279. return pwm;
  1280. ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
  1281. if (ret)
  1282. return ERR_PTR(ret);
  1283. return pwm;
  1284. }
  1285. EXPORT_SYMBOL_GPL(devm_pwm_get);
  1286. /**
  1287. * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node
  1288. * @dev: device for PWM consumer
  1289. * @fwnode: firmware node to get the PWM from
  1290. * @con_id: consumer name
  1291. *
  1292. * Returns the PWM device parsed from the firmware node. See of_pwm_get() and
  1293. * acpi_pwm_get() for a detailed description.
  1294. *
  1295. * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded
  1296. * error code on failure.
  1297. */
  1298. struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
  1299. struct fwnode_handle *fwnode,
  1300. const char *con_id)
  1301. {
  1302. struct pwm_device *pwm = ERR_PTR(-ENODEV);
  1303. int ret;
  1304. if (is_of_node(fwnode))
  1305. pwm = of_pwm_get(dev, to_of_node(fwnode), con_id);
  1306. else if (is_acpi_node(fwnode))
  1307. pwm = acpi_pwm_get(fwnode);
  1308. if (IS_ERR(pwm))
  1309. return pwm;
  1310. ret = devm_add_action_or_reset(dev, devm_pwm_release, pwm);
  1311. if (ret)
  1312. return ERR_PTR(ret);
  1313. return pwm;
  1314. }
  1315. EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get);
  1316. static void pwm_dbg_show(struct pwm_chip *chip, struct seq_file *s)
  1317. {
  1318. unsigned int i;
  1319. for (i = 0; i < chip->npwm; i++) {
  1320. struct pwm_device *pwm = &chip->pwms[i];
  1321. struct pwm_state state;
  1322. pwm_get_state(pwm, &state);
  1323. seq_printf(s, " pwm-%-3d (%-20.20s):", i, pwm->label);
  1324. if (test_bit(PWMF_REQUESTED, &pwm->flags))
  1325. seq_puts(s, " requested");
  1326. if (state.enabled)
  1327. seq_puts(s, " enabled");
  1328. seq_printf(s, " period: %llu ns", state.period);
  1329. seq_printf(s, " duty: %llu ns", state.duty_cycle);
  1330. seq_printf(s, " polarity: %s",
  1331. state.polarity ? "inverse" : "normal");
  1332. if (state.usage_power)
  1333. seq_puts(s, " usage_power");
  1334. seq_puts(s, "\n");
  1335. }
  1336. }
  1337. static void *pwm_seq_start(struct seq_file *s, loff_t *pos)
  1338. {
  1339. unsigned long id = *pos;
  1340. void *ret;
  1341. mutex_lock(&pwm_lock);
  1342. s->private = "";
  1343. ret = idr_get_next_ul(&pwm_chips, &id);
  1344. *pos = id;
  1345. return ret;
  1346. }
  1347. static void *pwm_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1348. {
  1349. unsigned long id = *pos + 1;
  1350. void *ret;
  1351. s->private = "\n";
  1352. ret = idr_get_next_ul(&pwm_chips, &id);
  1353. *pos = id;
  1354. return ret;
  1355. }
  1356. static void pwm_seq_stop(struct seq_file *s, void *v)
  1357. {
  1358. mutex_unlock(&pwm_lock);
  1359. }
  1360. static int pwm_seq_show(struct seq_file *s, void *v)
  1361. {
  1362. struct pwm_chip *chip = v;
  1363. seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n",
  1364. (char *)s->private, chip->id,
  1365. pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus",
  1366. dev_name(pwmchip_parent(chip)), chip->npwm,
  1367. (chip->npwm != 1) ? "s" : "");
  1368. pwm_dbg_show(chip, s);
  1369. return 0;
  1370. }
  1371. static const struct seq_operations pwm_debugfs_sops = {
  1372. .start = pwm_seq_start,
  1373. .next = pwm_seq_next,
  1374. .stop = pwm_seq_stop,
  1375. .show = pwm_seq_show,
  1376. };
  1377. DEFINE_SEQ_ATTRIBUTE(pwm_debugfs);
  1378. static int __init pwm_init(void)
  1379. {
  1380. int ret;
  1381. ret = class_register(&pwm_class);
  1382. if (ret) {
  1383. pr_err("Failed to initialize PWM class (%pe)\n", ERR_PTR(ret));
  1384. return ret;
  1385. }
  1386. if (IS_ENABLED(CONFIG_DEBUG_FS))
  1387. debugfs_create_file("pwm", 0444, NULL, NULL, &pwm_debugfs_fops);
  1388. return 0;
  1389. }
  1390. subsys_initcall(pwm_init);