pwm-samsung.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2007 Ben Dooks
  4. * Copyright (c) 2008 Simtec Electronics
  5. * Ben Dooks <ben@simtec.co.uk>, <ben-linux@fluff.org>
  6. * Copyright (c) 2013 Tomasz Figa <tomasz.figa@gmail.com>
  7. * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  8. *
  9. * PWM driver for Samsung SoCs
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/export.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pwm.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/time.h>
  24. /* For struct samsung_timer_variant and samsung_pwm_lock. */
  25. #include <clocksource/samsung_pwm.h>
  26. #define REG_TCFG0 0x00
  27. #define REG_TCFG1 0x04
  28. #define REG_TCON 0x08
  29. #define REG_TCNTB(chan) (0x0c + ((chan) * 0xc))
  30. #define REG_TCMPB(chan) (0x10 + ((chan) * 0xc))
  31. #define TCFG0_PRESCALER_MASK 0xff
  32. #define TCFG0_PRESCALER1_SHIFT 8
  33. #define TCFG1_MUX_MASK 0xf
  34. #define TCFG1_SHIFT(chan) (4 * (chan))
  35. /*
  36. * Each channel occupies 4 bits in TCON register, but there is a gap of 4
  37. * bits (one channel) after channel 0, so channels have different numbering
  38. * when accessing TCON register. See to_tcon_channel() function.
  39. *
  40. * In addition, the location of autoreload bit for channel 4 (TCON channel 5)
  41. * in its set of bits is 2 as opposed to 3 for other channels.
  42. */
  43. #define TCON_START(chan) BIT(4 * (chan) + 0)
  44. #define TCON_MANUALUPDATE(chan) BIT(4 * (chan) + 1)
  45. #define TCON_INVERT(chan) BIT(4 * (chan) + 2)
  46. #define _TCON_AUTORELOAD(chan) BIT(4 * (chan) + 3)
  47. #define _TCON_AUTORELOAD4(chan) BIT(4 * (chan) + 2)
  48. #define TCON_AUTORELOAD(chan) \
  49. ((chan < 5) ? _TCON_AUTORELOAD(chan) : _TCON_AUTORELOAD4(chan))
  50. /**
  51. * struct samsung_pwm_channel - private data of PWM channel
  52. * @period_ns: current period in nanoseconds programmed to the hardware
  53. * @duty_ns: current duty time in nanoseconds programmed to the hardware
  54. * @tin_ns: time of one timer tick in nanoseconds with current timer rate
  55. */
  56. struct samsung_pwm_channel {
  57. u32 period_ns;
  58. u32 duty_ns;
  59. u32 tin_ns;
  60. };
  61. /**
  62. * struct samsung_pwm_chip - private data of PWM chip
  63. * @variant: local copy of hardware variant data
  64. * @inverter_mask: inverter status for all channels - one bit per channel
  65. * @disabled_mask: disabled status for all channels - one bit per channel
  66. * @base: base address of mapped PWM registers
  67. * @base_clk: base clock used to drive the timers
  68. * @tclk0: external clock 0 (can be ERR_PTR if not present)
  69. * @tclk1: external clock 1 (can be ERR_PTR if not present)
  70. * @channel: per channel driver data
  71. */
  72. struct samsung_pwm_chip {
  73. struct samsung_pwm_variant variant;
  74. u8 inverter_mask;
  75. u8 disabled_mask;
  76. void __iomem *base;
  77. struct clk *base_clk;
  78. struct clk *tclk0;
  79. struct clk *tclk1;
  80. struct samsung_pwm_channel channel[SAMSUNG_PWM_NUM];
  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 pwmchip_get_drvdata(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_manual_update(struct samsung_pwm_chip *our_chip,
  107. struct pwm_device *pwm)
  108. {
  109. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  110. u32 tcon;
  111. tcon = readl(our_chip->base + REG_TCON);
  112. tcon |= TCON_MANUALUPDATE(tcon_chan);
  113. writel(tcon, our_chip->base + REG_TCON);
  114. tcon &= ~TCON_MANUALUPDATE(tcon_chan);
  115. writel(tcon, our_chip->base + REG_TCON);
  116. }
  117. static void pwm_samsung_set_divisor(struct samsung_pwm_chip *our_chip,
  118. unsigned int channel, u8 divisor)
  119. {
  120. u8 shift = TCFG1_SHIFT(channel);
  121. unsigned long flags;
  122. u32 reg;
  123. u8 bits;
  124. bits = (fls(divisor) - 1) - our_chip->variant.div_base;
  125. spin_lock_irqsave(&samsung_pwm_lock, flags);
  126. reg = readl(our_chip->base + REG_TCFG1);
  127. reg &= ~(TCFG1_MUX_MASK << shift);
  128. reg |= bits << shift;
  129. writel(reg, our_chip->base + REG_TCFG1);
  130. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  131. }
  132. static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *our_chip, unsigned int chan)
  133. {
  134. struct samsung_pwm_variant *variant = &our_chip->variant;
  135. u32 reg;
  136. reg = readl(our_chip->base + REG_TCFG1);
  137. reg >>= TCFG1_SHIFT(chan);
  138. reg &= TCFG1_MUX_MASK;
  139. return (BIT(reg) & variant->tclk_mask) == 0;
  140. }
  141. static unsigned long pwm_samsung_get_tin_rate(struct samsung_pwm_chip *our_chip,
  142. unsigned int chan)
  143. {
  144. unsigned long rate;
  145. u32 reg;
  146. rate = clk_get_rate(our_chip->base_clk);
  147. reg = readl(our_chip->base + REG_TCFG0);
  148. if (chan >= 2)
  149. reg >>= TCFG0_PRESCALER1_SHIFT;
  150. reg &= TCFG0_PRESCALER_MASK;
  151. return rate / (reg + 1);
  152. }
  153. static unsigned long pwm_samsung_calc_tin(struct pwm_chip *chip,
  154. unsigned int chan, unsigned long freq)
  155. {
  156. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  157. struct samsung_pwm_variant *variant = &our_chip->variant;
  158. unsigned long rate;
  159. struct clk *clk;
  160. u8 div;
  161. if (!pwm_samsung_is_tdiv(our_chip, chan)) {
  162. clk = (chan < 2) ? our_chip->tclk0 : our_chip->tclk1;
  163. if (!IS_ERR(clk)) {
  164. rate = clk_get_rate(clk);
  165. if (rate)
  166. return rate;
  167. }
  168. dev_warn(pwmchip_parent(chip),
  169. "tclk of PWM %d is inoperational, using tdiv\n", chan);
  170. }
  171. rate = pwm_samsung_get_tin_rate(our_chip, chan);
  172. dev_dbg(pwmchip_parent(chip), "tin parent at %lu\n", rate);
  173. /*
  174. * Compare minimum PWM frequency that can be achieved with possible
  175. * divider settings and choose the lowest divisor that can generate
  176. * frequencies lower than requested.
  177. */
  178. if (variant->bits < 32) {
  179. /* Only for s3c24xx */
  180. for (div = variant->div_base; div < 4; ++div)
  181. if ((rate >> (variant->bits + div)) < freq)
  182. break;
  183. } else {
  184. /*
  185. * Other variants have enough counter bits to generate any
  186. * requested rate, so no need to check higher divisors.
  187. */
  188. div = variant->div_base;
  189. }
  190. pwm_samsung_set_divisor(our_chip, chan, BIT(div));
  191. return rate >> div;
  192. }
  193. static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
  194. {
  195. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  196. if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
  197. dev_warn(pwmchip_parent(chip),
  198. "tried to request PWM channel %d without output\n",
  199. pwm->hwpwm);
  200. return -EINVAL;
  201. }
  202. memset(&our_chip->channel[pwm->hwpwm], 0, sizeof(our_chip->channel[pwm->hwpwm]));
  203. return 0;
  204. }
  205. static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  206. {
  207. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  208. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  209. unsigned long flags;
  210. u32 tcon;
  211. spin_lock_irqsave(&samsung_pwm_lock, flags);
  212. tcon = readl(our_chip->base + REG_TCON);
  213. tcon &= ~TCON_START(tcon_chan);
  214. tcon |= TCON_MANUALUPDATE(tcon_chan);
  215. writel(tcon, our_chip->base + REG_TCON);
  216. tcon &= ~TCON_MANUALUPDATE(tcon_chan);
  217. tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
  218. writel(tcon, our_chip->base + REG_TCON);
  219. our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
  220. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  221. return 0;
  222. }
  223. static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  224. {
  225. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  226. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  227. unsigned long flags;
  228. u32 tcon;
  229. spin_lock_irqsave(&samsung_pwm_lock, flags);
  230. tcon = readl(our_chip->base + REG_TCON);
  231. tcon &= ~TCON_AUTORELOAD(tcon_chan);
  232. writel(tcon, our_chip->base + REG_TCON);
  233. /*
  234. * In case the PWM is at 100% duty cycle, force a manual
  235. * update to prevent the signal from staying high.
  236. */
  237. if (readl(our_chip->base + REG_TCMPB(pwm->hwpwm)) == (u32)-1U)
  238. __pwm_samsung_manual_update(our_chip, pwm);
  239. our_chip->disabled_mask |= BIT(pwm->hwpwm);
  240. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  241. }
  242. static void pwm_samsung_manual_update(struct samsung_pwm_chip *our_chip,
  243. struct pwm_device *pwm)
  244. {
  245. unsigned long flags;
  246. spin_lock_irqsave(&samsung_pwm_lock, flags);
  247. __pwm_samsung_manual_update(our_chip, pwm);
  248. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  249. }
  250. static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
  251. int duty_ns, int period_ns, bool force_period)
  252. {
  253. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  254. struct samsung_pwm_channel *chan = &our_chip->channel[pwm->hwpwm];
  255. u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
  256. tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
  257. oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
  258. /* We need tick count for calculation, not last tick. */
  259. ++tcnt;
  260. /* Check to see if we are changing the clock rate of the PWM. */
  261. if (chan->period_ns != period_ns || force_period) {
  262. unsigned long tin_rate;
  263. u32 period;
  264. period = NSEC_PER_SEC / period_ns;
  265. dev_dbg(pwmchip_parent(chip), "duty_ns=%d, period_ns=%d (%u)\n",
  266. duty_ns, period_ns, period);
  267. tin_rate = pwm_samsung_calc_tin(chip, pwm->hwpwm, period);
  268. dev_dbg(pwmchip_parent(chip), "tin_rate=%lu\n", tin_rate);
  269. tin_ns = NSEC_PER_SEC / tin_rate;
  270. tcnt = period_ns / tin_ns;
  271. }
  272. /* Period is too short. */
  273. if (tcnt <= 1)
  274. return -ERANGE;
  275. /* Note that counters count down. */
  276. tcmp = duty_ns / tin_ns;
  277. /* 0% duty is not available */
  278. if (!tcmp)
  279. ++tcmp;
  280. tcmp = tcnt - tcmp;
  281. /* Decrement to get tick numbers, instead of tick counts. */
  282. --tcnt;
  283. /* -1UL will give 100% duty. */
  284. --tcmp;
  285. dev_dbg(pwmchip_parent(chip), "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
  286. /* Update PWM registers. */
  287. writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
  288. writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
  289. /*
  290. * In case the PWM is currently at 100% duty cycle, force a manual
  291. * update to prevent the signal staying high if the PWM is disabled
  292. * shortly afer this update (before it autoreloaded the new values).
  293. */
  294. if (oldtcmp == (u32) -1) {
  295. dev_dbg(pwmchip_parent(chip), "Forcing manual update");
  296. pwm_samsung_manual_update(our_chip, pwm);
  297. }
  298. chan->period_ns = period_ns;
  299. chan->tin_ns = tin_ns;
  300. chan->duty_ns = duty_ns;
  301. return 0;
  302. }
  303. static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
  304. int duty_ns, int period_ns)
  305. {
  306. return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
  307. }
  308. static void pwm_samsung_set_invert(struct samsung_pwm_chip *our_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(our_chip->base + REG_TCON);
  316. if (invert) {
  317. our_chip->inverter_mask |= BIT(channel);
  318. tcon |= TCON_INVERT(tcon_chan);
  319. } else {
  320. our_chip->inverter_mask &= ~BIT(channel);
  321. tcon &= ~TCON_INVERT(tcon_chan);
  322. }
  323. writel(tcon, our_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 int pwm_samsung_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  337. const struct pwm_state *state)
  338. {
  339. int err, enabled = pwm->state.enabled;
  340. if (state->polarity != pwm->state.polarity) {
  341. if (enabled) {
  342. pwm_samsung_disable(chip, pwm);
  343. enabled = false;
  344. }
  345. err = pwm_samsung_set_polarity(chip, pwm, state->polarity);
  346. if (err)
  347. return err;
  348. }
  349. if (!state->enabled) {
  350. if (enabled)
  351. pwm_samsung_disable(chip, pwm);
  352. return 0;
  353. }
  354. /*
  355. * We currently avoid using 64bit arithmetic by using the
  356. * fact that anything faster than 1Hz is easily representable
  357. * by 32bits.
  358. */
  359. if (state->period > NSEC_PER_SEC)
  360. return -ERANGE;
  361. err = pwm_samsung_config(chip, pwm, state->duty_cycle, state->period);
  362. if (err)
  363. return err;
  364. if (!pwm->state.enabled)
  365. err = pwm_samsung_enable(chip, pwm);
  366. return err;
  367. }
  368. static const struct pwm_ops pwm_samsung_ops = {
  369. .request = pwm_samsung_request,
  370. .apply = pwm_samsung_apply,
  371. };
  372. #ifdef CONFIG_OF
  373. static const struct samsung_pwm_variant s3c24xx_variant = {
  374. .bits = 16,
  375. .div_base = 1,
  376. .has_tint_cstat = false,
  377. .tclk_mask = BIT(4),
  378. };
  379. static const struct samsung_pwm_variant s3c64xx_variant = {
  380. .bits = 32,
  381. .div_base = 0,
  382. .has_tint_cstat = true,
  383. .tclk_mask = BIT(7) | BIT(6) | BIT(5),
  384. };
  385. static const struct samsung_pwm_variant s5p64x0_variant = {
  386. .bits = 32,
  387. .div_base = 0,
  388. .has_tint_cstat = true,
  389. .tclk_mask = 0,
  390. };
  391. static const struct samsung_pwm_variant s5pc100_variant = {
  392. .bits = 32,
  393. .div_base = 0,
  394. .has_tint_cstat = true,
  395. .tclk_mask = BIT(5),
  396. };
  397. static const struct of_device_id samsung_pwm_matches[] = {
  398. { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
  399. { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
  400. { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
  401. { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
  402. { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
  403. {},
  404. };
  405. MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
  406. static int pwm_samsung_parse_dt(struct pwm_chip *chip)
  407. {
  408. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  409. struct device_node *np = pwmchip_parent(chip)->of_node;
  410. const struct of_device_id *match;
  411. u32 val;
  412. match = of_match_node(samsung_pwm_matches, np);
  413. if (!match)
  414. return -ENODEV;
  415. memcpy(&our_chip->variant, match->data, sizeof(our_chip->variant));
  416. of_property_for_each_u32(np, "samsung,pwm-outputs", val) {
  417. if (val >= SAMSUNG_PWM_NUM) {
  418. dev_err(pwmchip_parent(chip),
  419. "%s: invalid channel index in samsung,pwm-outputs property\n",
  420. __func__);
  421. continue;
  422. }
  423. our_chip->variant.output_mask |= BIT(val);
  424. }
  425. return 0;
  426. }
  427. #else
  428. static int pwm_samsung_parse_dt(struct pwm_chip *chip)
  429. {
  430. return -ENODEV;
  431. }
  432. #endif
  433. static int pwm_samsung_probe(struct platform_device *pdev)
  434. {
  435. struct device *dev = &pdev->dev;
  436. struct samsung_pwm_chip *our_chip;
  437. struct pwm_chip *chip;
  438. unsigned int chan;
  439. int ret;
  440. chip = devm_pwmchip_alloc(&pdev->dev, SAMSUNG_PWM_NUM, sizeof(*our_chip));
  441. if (IS_ERR(chip))
  442. return PTR_ERR(chip);
  443. our_chip = to_samsung_pwm_chip(chip);
  444. chip->ops = &pwm_samsung_ops;
  445. our_chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
  446. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  447. ret = pwm_samsung_parse_dt(chip);
  448. if (ret)
  449. return ret;
  450. } else {
  451. if (!pdev->dev.platform_data)
  452. return dev_err_probe(&pdev->dev, -EINVAL,
  453. "no platform data specified\n");
  454. memcpy(&our_chip->variant, pdev->dev.platform_data,
  455. sizeof(our_chip->variant));
  456. }
  457. our_chip->base = devm_platform_ioremap_resource(pdev, 0);
  458. if (IS_ERR(our_chip->base))
  459. return PTR_ERR(our_chip->base);
  460. our_chip->base_clk = devm_clk_get_enabled(&pdev->dev, "timers");
  461. if (IS_ERR(our_chip->base_clk))
  462. return dev_err_probe(dev, PTR_ERR(our_chip->base_clk),
  463. "failed to get timer base clk\n");
  464. for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
  465. if (our_chip->variant.output_mask & BIT(chan))
  466. pwm_samsung_set_invert(our_chip, chan, true);
  467. /* Following clocks are optional. */
  468. our_chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
  469. our_chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
  470. platform_set_drvdata(pdev, chip);
  471. ret = devm_pwmchip_add(&pdev->dev, chip);
  472. if (ret < 0)
  473. return dev_err_probe(dev, ret, "failed to register PWM chip\n");
  474. dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
  475. clk_get_rate(our_chip->base_clk),
  476. !IS_ERR(our_chip->tclk0) ? clk_get_rate(our_chip->tclk0) : 0,
  477. !IS_ERR(our_chip->tclk1) ? clk_get_rate(our_chip->tclk1) : 0);
  478. return 0;
  479. }
  480. static int pwm_samsung_resume(struct device *dev)
  481. {
  482. struct pwm_chip *chip = dev_get_drvdata(dev);
  483. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  484. unsigned int i;
  485. for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
  486. struct pwm_device *pwm = &chip->pwms[i];
  487. struct samsung_pwm_channel *chan = &our_chip->channel[i];
  488. if (!test_bit(PWMF_REQUESTED, &pwm->flags))
  489. continue;
  490. if (our_chip->variant.output_mask & BIT(i))
  491. pwm_samsung_set_invert(our_chip, i,
  492. our_chip->inverter_mask & BIT(i));
  493. if (chan->period_ns) {
  494. __pwm_samsung_config(chip, pwm, chan->duty_ns,
  495. chan->period_ns, true);
  496. /* needed to make PWM disable work on Odroid-XU3 */
  497. pwm_samsung_manual_update(our_chip, pwm);
  498. }
  499. if (our_chip->disabled_mask & BIT(i))
  500. pwm_samsung_disable(chip, pwm);
  501. else
  502. pwm_samsung_enable(chip, pwm);
  503. }
  504. return 0;
  505. }
  506. static DEFINE_SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
  507. static struct platform_driver pwm_samsung_driver = {
  508. .driver = {
  509. .name = "samsung-pwm",
  510. .pm = pm_ptr(&pwm_samsung_pm_ops),
  511. .of_match_table = of_match_ptr(samsung_pwm_matches),
  512. },
  513. .probe = pwm_samsung_probe,
  514. };
  515. module_platform_driver(pwm_samsung_driver);
  516. MODULE_DESCRIPTION("Samsung Pulse Width Modulator driver");
  517. MODULE_LICENSE("GPL");
  518. MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
  519. MODULE_ALIAS("platform:samsung-pwm");