pwm-samsung.c 17 KB

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