pwm-samsung.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Copyright (c) 2007 Ben Dooks
  3. * Copyright (c) 2008 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
  5. * Copyright (c) 2013 Tomasz Figa <tomasz.figa@gmail.com>
  6. *
  7. * PWM driver for Samsung SoCs
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. */
  13. #include <linux/bitops.h>
  14. #include <linux/clk.h>
  15. #include <linux/export.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pwm.h>
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/time.h>
  26. /* For struct samsung_timer_variant and samsung_pwm_lock. */
  27. #include <clocksource/samsung_pwm.h>
  28. #define REG_TCFG0 0x00
  29. #define REG_TCFG1 0x04
  30. #define REG_TCON 0x08
  31. #define REG_TCNTB(chan) (0x0c + ((chan) * 0xc))
  32. #define REG_TCMPB(chan) (0x10 + ((chan) * 0xc))
  33. #define TCFG0_PRESCALER_MASK 0xff
  34. #define TCFG0_PRESCALER1_SHIFT 8
  35. #define TCFG1_MUX_MASK 0xf
  36. #define TCFG1_SHIFT(chan) (4 * (chan))
  37. /*
  38. * Each channel occupies 4 bits in TCON register, but there is a gap of 4
  39. * bits (one channel) after channel 0, so channels have different numbering
  40. * when accessing TCON register. See to_tcon_channel() function.
  41. *
  42. * In addition, the location of autoreload bit for channel 4 (TCON channel 5)
  43. * in its set of bits is 2 as opposed to 3 for other channels.
  44. */
  45. #define TCON_START(chan) BIT(4 * (chan) + 0)
  46. #define TCON_MANUALUPDATE(chan) BIT(4 * (chan) + 1)
  47. #define TCON_INVERT(chan) BIT(4 * (chan) + 2)
  48. #define _TCON_AUTORELOAD(chan) BIT(4 * (chan) + 3)
  49. #define _TCON_AUTORELOAD4(chan) BIT(4 * (chan) + 2)
  50. #define TCON_AUTORELOAD(chan) \
  51. ((chan < 5) ? _TCON_AUTORELOAD(chan) : _TCON_AUTORELOAD4(chan))
  52. /**
  53. * struct samsung_pwm_channel - private data of PWM channel
  54. * @period_ns: current period in nanoseconds programmed to the hardware
  55. * @duty_ns: current duty time in nanoseconds programmed to the hardware
  56. * @tin_ns: time of one timer tick in nanoseconds with current timer rate
  57. */
  58. struct samsung_pwm_channel {
  59. u32 period_ns;
  60. u32 duty_ns;
  61. u32 tin_ns;
  62. };
  63. /**
  64. * struct samsung_pwm_chip - private data of PWM chip
  65. * @chip: generic PWM chip
  66. * @variant: local copy of hardware variant data
  67. * @inverter_mask: inverter status for all channels - one bit per channel
  68. * @base: base address of mapped PWM registers
  69. * @base_clk: base clock used to drive the timers
  70. * @tclk0: external clock 0 (can be ERR_PTR if not present)
  71. * @tclk1: external clock 1 (can be ERR_PTR if not present)
  72. */
  73. struct samsung_pwm_chip {
  74. struct pwm_chip chip;
  75. struct samsung_pwm_variant variant;
  76. u8 inverter_mask;
  77. void __iomem *base;
  78. struct clk *base_clk;
  79. struct clk *tclk0;
  80. struct clk *tclk1;
  81. };
  82. #ifndef CONFIG_CLKSRC_SAMSUNG_PWM
  83. /*
  84. * PWM block is shared between pwm-samsung and samsung_pwm_timer drivers
  85. * and some registers need access synchronization. If both drivers are
  86. * compiled in, the spinlock is defined in the clocksource driver,
  87. * otherwise following definition is used.
  88. *
  89. * Currently we do not need any more complex synchronization method
  90. * because all the supported SoCs contain only one instance of the PWM
  91. * IP. Should this change, both drivers will need to be modified to
  92. * properly synchronize accesses to particular instances.
  93. */
  94. static DEFINE_SPINLOCK(samsung_pwm_lock);
  95. #endif
  96. static inline
  97. struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip)
  98. {
  99. return container_of(chip, struct samsung_pwm_chip, chip);
  100. }
  101. static inline unsigned int to_tcon_channel(unsigned int channel)
  102. {
  103. /* TCON register has a gap of 4 bits (1 channel) after channel 0 */
  104. return (channel == 0) ? 0 : (channel + 1);
  105. }
  106. static void pwm_samsung_set_divisor(struct samsung_pwm_chip *pwm,
  107. unsigned int channel, u8 divisor)
  108. {
  109. u8 shift = TCFG1_SHIFT(channel);
  110. unsigned long flags;
  111. u32 reg;
  112. u8 bits;
  113. bits = (fls(divisor) - 1) - pwm->variant.div_base;
  114. spin_lock_irqsave(&samsung_pwm_lock, flags);
  115. reg = readl(pwm->base + REG_TCFG1);
  116. reg &= ~(TCFG1_MUX_MASK << shift);
  117. reg |= bits << shift;
  118. writel(reg, pwm->base + REG_TCFG1);
  119. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  120. }
  121. static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *chip, unsigned int chan)
  122. {
  123. struct samsung_pwm_variant *variant = &chip->variant;
  124. u32 reg;
  125. reg = readl(chip->base + REG_TCFG1);
  126. reg >>= TCFG1_SHIFT(chan);
  127. reg &= TCFG1_MUX_MASK;
  128. return (BIT(reg) & variant->tclk_mask) == 0;
  129. }
  130. static unsigned long pwm_samsung_get_tin_rate(struct samsung_pwm_chip *chip,
  131. unsigned int chan)
  132. {
  133. unsigned long rate;
  134. u32 reg;
  135. rate = clk_get_rate(chip->base_clk);
  136. reg = readl(chip->base + REG_TCFG0);
  137. if (chan >= 2)
  138. reg >>= TCFG0_PRESCALER1_SHIFT;
  139. reg &= TCFG0_PRESCALER_MASK;
  140. return rate / (reg + 1);
  141. }
  142. static unsigned long pwm_samsung_calc_tin(struct samsung_pwm_chip *chip,
  143. unsigned int chan, unsigned long freq)
  144. {
  145. struct samsung_pwm_variant *variant = &chip->variant;
  146. unsigned long rate;
  147. struct clk *clk;
  148. u8 div;
  149. if (!pwm_samsung_is_tdiv(chip, chan)) {
  150. clk = (chan < 2) ? chip->tclk0 : chip->tclk1;
  151. if (!IS_ERR(clk)) {
  152. rate = clk_get_rate(clk);
  153. if (rate)
  154. return rate;
  155. }
  156. dev_warn(chip->chip.dev,
  157. "tclk of PWM %d is inoperational, using tdiv\n", chan);
  158. }
  159. rate = pwm_samsung_get_tin_rate(chip, chan);
  160. dev_dbg(chip->chip.dev, "tin parent at %lu\n", rate);
  161. /*
  162. * Compare minimum PWM frequency that can be achieved with possible
  163. * divider settings and choose the lowest divisor that can generate
  164. * frequencies lower than requested.
  165. */
  166. if (variant->bits < 32) {
  167. /* Only for s3c24xx */
  168. for (div = variant->div_base; div < 4; ++div)
  169. if ((rate >> (variant->bits + div)) < freq)
  170. break;
  171. } else {
  172. /*
  173. * Other variants have enough counter bits to generate any
  174. * requested rate, so no need to check higher divisors.
  175. */
  176. div = variant->div_base;
  177. }
  178. pwm_samsung_set_divisor(chip, chan, BIT(div));
  179. return rate >> div;
  180. }
  181. static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
  182. {
  183. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  184. struct samsung_pwm_channel *our_chan;
  185. if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
  186. dev_warn(chip->dev,
  187. "tried to request PWM channel %d without output\n",
  188. pwm->hwpwm);
  189. return -EINVAL;
  190. }
  191. our_chan = devm_kzalloc(chip->dev, sizeof(*our_chan), GFP_KERNEL);
  192. if (!our_chan)
  193. return -ENOMEM;
  194. pwm_set_chip_data(pwm, our_chan);
  195. return 0;
  196. }
  197. static void pwm_samsung_free(struct pwm_chip *chip, struct pwm_device *pwm)
  198. {
  199. devm_kfree(chip->dev, pwm_get_chip_data(pwm));
  200. pwm_set_chip_data(pwm, NULL);
  201. }
  202. static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  203. {
  204. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  205. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  206. unsigned long flags;
  207. u32 tcon;
  208. spin_lock_irqsave(&samsung_pwm_lock, flags);
  209. tcon = readl(our_chip->base + REG_TCON);
  210. tcon &= ~TCON_START(tcon_chan);
  211. tcon |= TCON_MANUALUPDATE(tcon_chan);
  212. writel(tcon, our_chip->base + REG_TCON);
  213. tcon &= ~TCON_MANUALUPDATE(tcon_chan);
  214. tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
  215. writel(tcon, our_chip->base + REG_TCON);
  216. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  217. return 0;
  218. }
  219. static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  220. {
  221. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  222. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  223. unsigned long flags;
  224. u32 tcon;
  225. spin_lock_irqsave(&samsung_pwm_lock, flags);
  226. tcon = readl(our_chip->base + REG_TCON);
  227. tcon &= ~TCON_AUTORELOAD(tcon_chan);
  228. writel(tcon, our_chip->base + REG_TCON);
  229. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  230. }
  231. static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
  232. struct pwm_device *pwm)
  233. {
  234. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  235. u32 tcon;
  236. unsigned long flags;
  237. spin_lock_irqsave(&samsung_pwm_lock, flags);
  238. tcon = readl(chip->base + REG_TCON);
  239. tcon |= TCON_MANUALUPDATE(tcon_chan);
  240. writel(tcon, chip->base + REG_TCON);
  241. tcon &= ~TCON_MANUALUPDATE(tcon_chan);
  242. writel(tcon, chip->base + REG_TCON);
  243. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  244. }
  245. static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
  246. int duty_ns, int period_ns)
  247. {
  248. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  249. struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
  250. u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
  251. /*
  252. * We currently avoid using 64bit arithmetic by using the
  253. * fact that anything faster than 1Hz is easily representable
  254. * by 32bits.
  255. */
  256. if (period_ns > NSEC_PER_SEC)
  257. return -ERANGE;
  258. if (period_ns == chan->period_ns && duty_ns == chan->duty_ns)
  259. return 0;
  260. tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
  261. oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
  262. /* We need tick count for calculation, not last tick. */
  263. ++tcnt;
  264. /* Check to see if we are changing the clock rate of the PWM. */
  265. if (chan->period_ns != period_ns) {
  266. unsigned long tin_rate;
  267. u32 period;
  268. period = NSEC_PER_SEC / period_ns;
  269. dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
  270. duty_ns, period_ns, period);
  271. tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
  272. dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
  273. tin_ns = NSEC_PER_SEC / tin_rate;
  274. tcnt = period_ns / tin_ns;
  275. }
  276. /* Period is too short. */
  277. if (tcnt <= 1)
  278. return -ERANGE;
  279. /* Note that counters count down. */
  280. tcmp = duty_ns / tin_ns;
  281. /* 0% duty is not available */
  282. if (!tcmp)
  283. ++tcmp;
  284. tcmp = tcnt - tcmp;
  285. /* Decrement to get tick numbers, instead of tick counts. */
  286. --tcnt;
  287. /* -1UL will give 100% duty. */
  288. --tcmp;
  289. dev_dbg(our_chip->chip.dev,
  290. "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
  291. /* Update PWM registers. */
  292. writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
  293. writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
  294. /*
  295. * In case the PWM is currently at 100% duty cycle, force a manual
  296. * update to prevent the signal staying high if the PWM is disabled
  297. * shortly afer this update (before it autoreloaded the new values).
  298. */
  299. if (oldtcmp == (u32) -1) {
  300. dev_dbg(our_chip->chip.dev, "Forcing manual update");
  301. pwm_samsung_manual_update(our_chip, pwm);
  302. }
  303. chan->period_ns = period_ns;
  304. chan->tin_ns = tin_ns;
  305. chan->duty_ns = duty_ns;
  306. return 0;
  307. }
  308. static void pwm_samsung_set_invert(struct samsung_pwm_chip *chip,
  309. unsigned int channel, bool invert)
  310. {
  311. unsigned int tcon_chan = to_tcon_channel(channel);
  312. unsigned long flags;
  313. u32 tcon;
  314. spin_lock_irqsave(&samsung_pwm_lock, flags);
  315. tcon = readl(chip->base + REG_TCON);
  316. if (invert) {
  317. chip->inverter_mask |= BIT(channel);
  318. tcon |= TCON_INVERT(tcon_chan);
  319. } else {
  320. chip->inverter_mask &= ~BIT(channel);
  321. tcon &= ~TCON_INVERT(tcon_chan);
  322. }
  323. writel(tcon, chip->base + REG_TCON);
  324. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  325. }
  326. static int pwm_samsung_set_polarity(struct pwm_chip *chip,
  327. struct pwm_device *pwm,
  328. enum pwm_polarity polarity)
  329. {
  330. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  331. bool invert = (polarity == PWM_POLARITY_NORMAL);
  332. /* Inverted means normal in the hardware. */
  333. pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
  334. return 0;
  335. }
  336. static const struct pwm_ops pwm_samsung_ops = {
  337. .request = pwm_samsung_request,
  338. .free = pwm_samsung_free,
  339. .enable = pwm_samsung_enable,
  340. .disable = pwm_samsung_disable,
  341. .config = pwm_samsung_config,
  342. .set_polarity = pwm_samsung_set_polarity,
  343. .owner = THIS_MODULE,
  344. };
  345. #ifdef CONFIG_OF
  346. static const struct samsung_pwm_variant s3c24xx_variant = {
  347. .bits = 16,
  348. .div_base = 1,
  349. .has_tint_cstat = false,
  350. .tclk_mask = BIT(4),
  351. };
  352. static const struct samsung_pwm_variant s3c64xx_variant = {
  353. .bits = 32,
  354. .div_base = 0,
  355. .has_tint_cstat = true,
  356. .tclk_mask = BIT(7) | BIT(6) | BIT(5),
  357. };
  358. static const struct samsung_pwm_variant s5p64x0_variant = {
  359. .bits = 32,
  360. .div_base = 0,
  361. .has_tint_cstat = true,
  362. .tclk_mask = 0,
  363. };
  364. static const struct samsung_pwm_variant s5pc100_variant = {
  365. .bits = 32,
  366. .div_base = 0,
  367. .has_tint_cstat = true,
  368. .tclk_mask = BIT(5),
  369. };
  370. static const struct of_device_id samsung_pwm_matches[] = {
  371. { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
  372. { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
  373. { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
  374. { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
  375. { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
  376. {},
  377. };
  378. MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
  379. static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
  380. {
  381. struct device_node *np = chip->chip.dev->of_node;
  382. const struct of_device_id *match;
  383. struct property *prop;
  384. const __be32 *cur;
  385. u32 val;
  386. match = of_match_node(samsung_pwm_matches, np);
  387. if (!match)
  388. return -ENODEV;
  389. memcpy(&chip->variant, match->data, sizeof(chip->variant));
  390. of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
  391. if (val >= SAMSUNG_PWM_NUM) {
  392. dev_err(chip->chip.dev,
  393. "%s: invalid channel index in samsung,pwm-outputs property\n",
  394. __func__);
  395. continue;
  396. }
  397. chip->variant.output_mask |= BIT(val);
  398. }
  399. return 0;
  400. }
  401. #else
  402. static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
  403. {
  404. return -ENODEV;
  405. }
  406. #endif
  407. static int pwm_samsung_probe(struct platform_device *pdev)
  408. {
  409. struct device *dev = &pdev->dev;
  410. struct samsung_pwm_chip *chip;
  411. struct resource *res;
  412. unsigned int chan;
  413. int ret;
  414. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  415. if (chip == NULL)
  416. return -ENOMEM;
  417. chip->chip.dev = &pdev->dev;
  418. chip->chip.ops = &pwm_samsung_ops;
  419. chip->chip.base = -1;
  420. chip->chip.npwm = SAMSUNG_PWM_NUM;
  421. chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
  422. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  423. ret = pwm_samsung_parse_dt(chip);
  424. if (ret)
  425. return ret;
  426. chip->chip.of_xlate = of_pwm_xlate_with_flags;
  427. chip->chip.of_pwm_n_cells = 3;
  428. } else {
  429. if (!pdev->dev.platform_data) {
  430. dev_err(&pdev->dev, "no platform data specified\n");
  431. return -EINVAL;
  432. }
  433. memcpy(&chip->variant, pdev->dev.platform_data,
  434. sizeof(chip->variant));
  435. }
  436. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  437. chip->base = devm_ioremap_resource(&pdev->dev, res);
  438. if (IS_ERR(chip->base))
  439. return PTR_ERR(chip->base);
  440. chip->base_clk = devm_clk_get(&pdev->dev, "timers");
  441. if (IS_ERR(chip->base_clk)) {
  442. dev_err(dev, "failed to get timer base clk\n");
  443. return PTR_ERR(chip->base_clk);
  444. }
  445. ret = clk_prepare_enable(chip->base_clk);
  446. if (ret < 0) {
  447. dev_err(dev, "failed to enable base clock\n");
  448. return ret;
  449. }
  450. for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
  451. if (chip->variant.output_mask & BIT(chan))
  452. pwm_samsung_set_invert(chip, chan, true);
  453. /* Following clocks are optional. */
  454. chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
  455. chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
  456. platform_set_drvdata(pdev, chip);
  457. ret = pwmchip_add(&chip->chip);
  458. if (ret < 0) {
  459. dev_err(dev, "failed to register PWM chip\n");
  460. clk_disable_unprepare(chip->base_clk);
  461. return ret;
  462. }
  463. dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
  464. clk_get_rate(chip->base_clk),
  465. !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
  466. !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
  467. return 0;
  468. }
  469. static int pwm_samsung_remove(struct platform_device *pdev)
  470. {
  471. struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
  472. int ret;
  473. ret = pwmchip_remove(&chip->chip);
  474. if (ret < 0)
  475. return ret;
  476. clk_disable_unprepare(chip->base_clk);
  477. return 0;
  478. }
  479. #ifdef CONFIG_PM_SLEEP
  480. static int pwm_samsung_suspend(struct device *dev)
  481. {
  482. struct samsung_pwm_chip *chip = dev_get_drvdata(dev);
  483. unsigned int i;
  484. /*
  485. * No one preserves these values during suspend so reset them.
  486. * Otherwise driver leaves PWM unconfigured if same values are
  487. * passed to pwm_config() next time.
  488. */
  489. for (i = 0; i < SAMSUNG_PWM_NUM; ++i) {
  490. struct pwm_device *pwm = &chip->chip.pwms[i];
  491. struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
  492. if (!chan)
  493. continue;
  494. chan->period_ns = 0;
  495. chan->duty_ns = 0;
  496. }
  497. return 0;
  498. }
  499. static int pwm_samsung_resume(struct device *dev)
  500. {
  501. struct samsung_pwm_chip *chip = dev_get_drvdata(dev);
  502. unsigned int chan;
  503. /*
  504. * Inverter setting must be preserved across suspend/resume
  505. * as nobody really seems to configure it more than once.
  506. */
  507. for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan) {
  508. if (chip->variant.output_mask & BIT(chan))
  509. pwm_samsung_set_invert(chip, chan,
  510. chip->inverter_mask & BIT(chan));
  511. }
  512. return 0;
  513. }
  514. #endif
  515. static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, pwm_samsung_suspend,
  516. pwm_samsung_resume);
  517. static struct platform_driver pwm_samsung_driver = {
  518. .driver = {
  519. .name = "samsung-pwm",
  520. .pm = &pwm_samsung_pm_ops,
  521. .of_match_table = of_match_ptr(samsung_pwm_matches),
  522. },
  523. .probe = pwm_samsung_probe,
  524. .remove = pwm_samsung_remove,
  525. };
  526. module_platform_driver(pwm_samsung_driver);
  527. MODULE_LICENSE("GPL");
  528. MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
  529. MODULE_ALIAS("platform:samsung-pwm");