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. pwm_set_chip_data(pwm, NULL);
  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. our_chip->disabled_mask |= BIT(pwm->hwpwm);
  234. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  235. }
  236. static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
  237. struct pwm_device *pwm)
  238. {
  239. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  240. u32 tcon;
  241. unsigned long flags;
  242. spin_lock_irqsave(&samsung_pwm_lock, flags);
  243. tcon = readl(chip->base + REG_TCON);
  244. tcon |= TCON_MANUALUPDATE(tcon_chan);
  245. writel(tcon, chip->base + REG_TCON);
  246. tcon &= ~TCON_MANUALUPDATE(tcon_chan);
  247. writel(tcon, chip->base + REG_TCON);
  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 = pwm_get_chip_data(pwm);
  255. u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
  256. /*
  257. * We currently avoid using 64bit arithmetic by using the
  258. * fact that anything faster than 1Hz is easily representable
  259. * by 32bits.
  260. */
  261. if (period_ns > NSEC_PER_SEC)
  262. return -ERANGE;
  263. tcnt = readl(our_chip->base + REG_TCNTB(pwm->hwpwm));
  264. oldtcmp = readl(our_chip->base + REG_TCMPB(pwm->hwpwm));
  265. /* We need tick count for calculation, not last tick. */
  266. ++tcnt;
  267. /* Check to see if we are changing the clock rate of the PWM. */
  268. if (chan->period_ns != period_ns || force_period) {
  269. unsigned long tin_rate;
  270. u32 period;
  271. period = NSEC_PER_SEC / period_ns;
  272. dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n",
  273. duty_ns, period_ns, period);
  274. tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period);
  275. dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate);
  276. tin_ns = NSEC_PER_SEC / tin_rate;
  277. tcnt = period_ns / tin_ns;
  278. }
  279. /* Period is too short. */
  280. if (tcnt <= 1)
  281. return -ERANGE;
  282. /* Note that counters count down. */
  283. tcmp = duty_ns / tin_ns;
  284. /* 0% duty is not available */
  285. if (!tcmp)
  286. ++tcmp;
  287. tcmp = tcnt - tcmp;
  288. /* Decrement to get tick numbers, instead of tick counts. */
  289. --tcnt;
  290. /* -1UL will give 100% duty. */
  291. --tcmp;
  292. dev_dbg(our_chip->chip.dev,
  293. "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt);
  294. /* Update PWM registers. */
  295. writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm));
  296. writel(tcmp, our_chip->base + REG_TCMPB(pwm->hwpwm));
  297. /*
  298. * In case the PWM is currently at 100% duty cycle, force a manual
  299. * update to prevent the signal staying high if the PWM is disabled
  300. * shortly afer this update (before it autoreloaded the new values).
  301. */
  302. if (oldtcmp == (u32) -1) {
  303. dev_dbg(our_chip->chip.dev, "Forcing manual update");
  304. pwm_samsung_manual_update(our_chip, pwm);
  305. }
  306. chan->period_ns = period_ns;
  307. chan->tin_ns = tin_ns;
  308. chan->duty_ns = duty_ns;
  309. return 0;
  310. }
  311. static int pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
  312. int duty_ns, int period_ns)
  313. {
  314. return __pwm_samsung_config(chip, pwm, duty_ns, period_ns, false);
  315. }
  316. static void pwm_samsung_set_invert(struct samsung_pwm_chip *chip,
  317. unsigned int channel, bool invert)
  318. {
  319. unsigned int tcon_chan = to_tcon_channel(channel);
  320. unsigned long flags;
  321. u32 tcon;
  322. spin_lock_irqsave(&samsung_pwm_lock, flags);
  323. tcon = readl(chip->base + REG_TCON);
  324. if (invert) {
  325. chip->inverter_mask |= BIT(channel);
  326. tcon |= TCON_INVERT(tcon_chan);
  327. } else {
  328. chip->inverter_mask &= ~BIT(channel);
  329. tcon &= ~TCON_INVERT(tcon_chan);
  330. }
  331. writel(tcon, chip->base + REG_TCON);
  332. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  333. }
  334. static int pwm_samsung_set_polarity(struct pwm_chip *chip,
  335. struct pwm_device *pwm,
  336. enum pwm_polarity polarity)
  337. {
  338. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  339. bool invert = (polarity == PWM_POLARITY_NORMAL);
  340. /* Inverted means normal in the hardware. */
  341. pwm_samsung_set_invert(our_chip, pwm->hwpwm, invert);
  342. return 0;
  343. }
  344. static const struct pwm_ops pwm_samsung_ops = {
  345. .request = pwm_samsung_request,
  346. .free = pwm_samsung_free,
  347. .enable = pwm_samsung_enable,
  348. .disable = pwm_samsung_disable,
  349. .config = pwm_samsung_config,
  350. .set_polarity = pwm_samsung_set_polarity,
  351. .owner = THIS_MODULE,
  352. };
  353. #ifdef CONFIG_OF
  354. static const struct samsung_pwm_variant s3c24xx_variant = {
  355. .bits = 16,
  356. .div_base = 1,
  357. .has_tint_cstat = false,
  358. .tclk_mask = BIT(4),
  359. };
  360. static const struct samsung_pwm_variant s3c64xx_variant = {
  361. .bits = 32,
  362. .div_base = 0,
  363. .has_tint_cstat = true,
  364. .tclk_mask = BIT(7) | BIT(6) | BIT(5),
  365. };
  366. static const struct samsung_pwm_variant s5p64x0_variant = {
  367. .bits = 32,
  368. .div_base = 0,
  369. .has_tint_cstat = true,
  370. .tclk_mask = 0,
  371. };
  372. static const struct samsung_pwm_variant s5pc100_variant = {
  373. .bits = 32,
  374. .div_base = 0,
  375. .has_tint_cstat = true,
  376. .tclk_mask = BIT(5),
  377. };
  378. static const struct of_device_id samsung_pwm_matches[] = {
  379. { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
  380. { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
  381. { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
  382. { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
  383. { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
  384. {},
  385. };
  386. MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
  387. static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
  388. {
  389. struct device_node *np = chip->chip.dev->of_node;
  390. const struct of_device_id *match;
  391. struct property *prop;
  392. const __be32 *cur;
  393. u32 val;
  394. match = of_match_node(samsung_pwm_matches, np);
  395. if (!match)
  396. return -ENODEV;
  397. memcpy(&chip->variant, match->data, sizeof(chip->variant));
  398. of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
  399. if (val >= SAMSUNG_PWM_NUM) {
  400. dev_err(chip->chip.dev,
  401. "%s: invalid channel index in samsung,pwm-outputs property\n",
  402. __func__);
  403. continue;
  404. }
  405. chip->variant.output_mask |= BIT(val);
  406. }
  407. return 0;
  408. }
  409. #else
  410. static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
  411. {
  412. return -ENODEV;
  413. }
  414. #endif
  415. static int pwm_samsung_probe(struct platform_device *pdev)
  416. {
  417. struct device *dev = &pdev->dev;
  418. struct samsung_pwm_chip *chip;
  419. struct resource *res;
  420. unsigned int chan;
  421. int ret;
  422. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  423. if (chip == NULL)
  424. return -ENOMEM;
  425. chip->chip.dev = &pdev->dev;
  426. chip->chip.ops = &pwm_samsung_ops;
  427. chip->chip.base = -1;
  428. chip->chip.npwm = SAMSUNG_PWM_NUM;
  429. chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
  430. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  431. ret = pwm_samsung_parse_dt(chip);
  432. if (ret)
  433. return ret;
  434. chip->chip.of_xlate = of_pwm_xlate_with_flags;
  435. chip->chip.of_pwm_n_cells = 3;
  436. } else {
  437. if (!pdev->dev.platform_data) {
  438. dev_err(&pdev->dev, "no platform data specified\n");
  439. return -EINVAL;
  440. }
  441. memcpy(&chip->variant, pdev->dev.platform_data,
  442. sizeof(chip->variant));
  443. }
  444. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  445. chip->base = devm_ioremap_resource(&pdev->dev, res);
  446. if (IS_ERR(chip->base))
  447. return PTR_ERR(chip->base);
  448. chip->base_clk = devm_clk_get(&pdev->dev, "timers");
  449. if (IS_ERR(chip->base_clk)) {
  450. dev_err(dev, "failed to get timer base clk\n");
  451. return PTR_ERR(chip->base_clk);
  452. }
  453. ret = clk_prepare_enable(chip->base_clk);
  454. if (ret < 0) {
  455. dev_err(dev, "failed to enable base clock\n");
  456. return ret;
  457. }
  458. for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
  459. if (chip->variant.output_mask & BIT(chan))
  460. pwm_samsung_set_invert(chip, chan, true);
  461. /* Following clocks are optional. */
  462. chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
  463. chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
  464. platform_set_drvdata(pdev, chip);
  465. ret = pwmchip_add(&chip->chip);
  466. if (ret < 0) {
  467. dev_err(dev, "failed to register PWM chip\n");
  468. clk_disable_unprepare(chip->base_clk);
  469. return ret;
  470. }
  471. dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
  472. clk_get_rate(chip->base_clk),
  473. !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
  474. !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
  475. return 0;
  476. }
  477. static int pwm_samsung_remove(struct platform_device *pdev)
  478. {
  479. struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
  480. int ret;
  481. ret = pwmchip_remove(&chip->chip);
  482. if (ret < 0)
  483. return ret;
  484. clk_disable_unprepare(chip->base_clk);
  485. return 0;
  486. }
  487. #ifdef CONFIG_PM_SLEEP
  488. static int pwm_samsung_resume(struct device *dev)
  489. {
  490. struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev);
  491. struct pwm_chip *chip = &our_chip->chip;
  492. unsigned int i;
  493. for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
  494. struct pwm_device *pwm = &chip->pwms[i];
  495. struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
  496. if (!chan)
  497. continue;
  498. if (our_chip->variant.output_mask & BIT(i))
  499. pwm_samsung_set_invert(our_chip, i,
  500. our_chip->inverter_mask & BIT(i));
  501. if (chan->period_ns) {
  502. __pwm_samsung_config(chip, pwm, chan->duty_ns,
  503. chan->period_ns, true);
  504. /* needed to make PWM disable work on Odroid-XU3 */
  505. pwm_samsung_manual_update(our_chip, pwm);
  506. }
  507. if (our_chip->disabled_mask & BIT(i))
  508. pwm_samsung_disable(chip, pwm);
  509. else
  510. pwm_samsung_enable(chip, pwm);
  511. }
  512. return 0;
  513. }
  514. #endif
  515. static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
  516. static struct platform_driver pwm_samsung_driver = {
  517. .driver = {
  518. .name = "samsung-pwm",
  519. .pm = &pwm_samsung_pm_ops,
  520. .of_match_table = of_match_ptr(samsung_pwm_matches),
  521. },
  522. .probe = pwm_samsung_probe,
  523. .remove = pwm_samsung_remove,
  524. };
  525. module_platform_driver(pwm_samsung_driver);
  526. MODULE_LICENSE("GPL");
  527. MODULE_AUTHOR("Tomasz Figa <tomasz.figa@gmail.com>");
  528. MODULE_ALIAS("platform:samsung-pwm");