pwm-samsung.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2007 Ben Dooks
  4. * Copyright (c) 2008 Simtec Electronics
  5. * Ben Dooks <[email protected]>, <[email protected]>
  6. * Copyright (c) 2013 Tomasz Figa <[email protected]>
  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. * @chip: generic PWM chip
  64. * @variant: local copy of hardware variant data
  65. * @inverter_mask: inverter status for all channels - one bit per channel
  66. * @disabled_mask: disabled status for all channels - one bit per channel
  67. * @base: base address of mapped PWM registers
  68. * @base_clk: base clock used to drive the timers
  69. * @tclk0: external clock 0 (can be ERR_PTR if not present)
  70. * @tclk1: external clock 1 (can be ERR_PTR if not present)
  71. */
  72. struct samsung_pwm_chip {
  73. struct pwm_chip chip;
  74. struct samsung_pwm_variant variant;
  75. u8 inverter_mask;
  76. u8 disabled_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_manual_update(struct samsung_pwm_chip *chip,
  107. struct pwm_device *pwm)
  108. {
  109. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  110. u32 tcon;
  111. tcon = readl(chip->base + REG_TCON);
  112. tcon |= TCON_MANUALUPDATE(tcon_chan);
  113. writel(tcon, chip->base + REG_TCON);
  114. tcon &= ~TCON_MANUALUPDATE(tcon_chan);
  115. writel(tcon, chip->base + REG_TCON);
  116. }
  117. static void pwm_samsung_set_divisor(struct samsung_pwm_chip *pwm,
  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) - pwm->variant.div_base;
  125. spin_lock_irqsave(&samsung_pwm_lock, flags);
  126. reg = readl(pwm->base + REG_TCFG1);
  127. reg &= ~(TCFG1_MUX_MASK << shift);
  128. reg |= bits << shift;
  129. writel(reg, pwm->base + REG_TCFG1);
  130. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  131. }
  132. static int pwm_samsung_is_tdiv(struct samsung_pwm_chip *chip, unsigned int chan)
  133. {
  134. struct samsung_pwm_variant *variant = &chip->variant;
  135. u32 reg;
  136. reg = readl(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 *chip,
  142. unsigned int chan)
  143. {
  144. unsigned long rate;
  145. u32 reg;
  146. rate = clk_get_rate(chip->base_clk);
  147. reg = readl(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 samsung_pwm_chip *chip,
  154. unsigned int chan, unsigned long freq)
  155. {
  156. struct samsung_pwm_variant *variant = &chip->variant;
  157. unsigned long rate;
  158. struct clk *clk;
  159. u8 div;
  160. if (!pwm_samsung_is_tdiv(chip, chan)) {
  161. clk = (chan < 2) ? chip->tclk0 : chip->tclk1;
  162. if (!IS_ERR(clk)) {
  163. rate = clk_get_rate(clk);
  164. if (rate)
  165. return rate;
  166. }
  167. dev_warn(chip->chip.dev,
  168. "tclk of PWM %d is inoperational, using tdiv\n", chan);
  169. }
  170. rate = pwm_samsung_get_tin_rate(chip, chan);
  171. dev_dbg(chip->chip.dev, "tin parent at %lu\n", rate);
  172. /*
  173. * Compare minimum PWM frequency that can be achieved with possible
  174. * divider settings and choose the lowest divisor that can generate
  175. * frequencies lower than requested.
  176. */
  177. if (variant->bits < 32) {
  178. /* Only for s3c24xx */
  179. for (div = variant->div_base; div < 4; ++div)
  180. if ((rate >> (variant->bits + div)) < freq)
  181. break;
  182. } else {
  183. /*
  184. * Other variants have enough counter bits to generate any
  185. * requested rate, so no need to check higher divisors.
  186. */
  187. div = variant->div_base;
  188. }
  189. pwm_samsung_set_divisor(chip, chan, BIT(div));
  190. return rate >> div;
  191. }
  192. static int pwm_samsung_request(struct pwm_chip *chip, struct pwm_device *pwm)
  193. {
  194. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  195. struct samsung_pwm_channel *our_chan;
  196. if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) {
  197. dev_warn(chip->dev,
  198. "tried to request PWM channel %d without output\n",
  199. pwm->hwpwm);
  200. return -EINVAL;
  201. }
  202. our_chan = kzalloc(sizeof(*our_chan), GFP_KERNEL);
  203. if (!our_chan)
  204. return -ENOMEM;
  205. pwm_set_chip_data(pwm, our_chan);
  206. return 0;
  207. }
  208. static void pwm_samsung_free(struct pwm_chip *chip, struct pwm_device *pwm)
  209. {
  210. kfree(pwm_get_chip_data(pwm));
  211. }
  212. static int pwm_samsung_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  213. {
  214. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  215. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  216. unsigned long flags;
  217. u32 tcon;
  218. spin_lock_irqsave(&samsung_pwm_lock, flags);
  219. tcon = readl(our_chip->base + REG_TCON);
  220. tcon &= ~TCON_START(tcon_chan);
  221. tcon |= TCON_MANUALUPDATE(tcon_chan);
  222. writel(tcon, our_chip->base + REG_TCON);
  223. tcon &= ~TCON_MANUALUPDATE(tcon_chan);
  224. tcon |= TCON_START(tcon_chan) | TCON_AUTORELOAD(tcon_chan);
  225. writel(tcon, our_chip->base + REG_TCON);
  226. our_chip->disabled_mask &= ~BIT(pwm->hwpwm);
  227. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  228. return 0;
  229. }
  230. static void pwm_samsung_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  231. {
  232. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  233. unsigned int tcon_chan = to_tcon_channel(pwm->hwpwm);
  234. unsigned long flags;
  235. u32 tcon;
  236. spin_lock_irqsave(&samsung_pwm_lock, flags);
  237. tcon = readl(our_chip->base + REG_TCON);
  238. tcon &= ~TCON_AUTORELOAD(tcon_chan);
  239. writel(tcon, our_chip->base + REG_TCON);
  240. /*
  241. * In case the PWM is at 100% duty cycle, force a manual
  242. * update to prevent the signal from staying high.
  243. */
  244. if (readl(our_chip->base + REG_TCMPB(pwm->hwpwm)) == (u32)-1U)
  245. __pwm_samsung_manual_update(our_chip, pwm);
  246. our_chip->disabled_mask |= BIT(pwm->hwpwm);
  247. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  248. }
  249. static void pwm_samsung_manual_update(struct samsung_pwm_chip *chip,
  250. struct pwm_device *pwm)
  251. {
  252. unsigned long flags;
  253. spin_lock_irqsave(&samsung_pwm_lock, flags);
  254. __pwm_samsung_manual_update(chip, pwm);
  255. spin_unlock_irqrestore(&samsung_pwm_lock, flags);
  256. }
  257. static int __pwm_samsung_config(struct pwm_chip *chip, struct pwm_device *pwm,
  258. int duty_ns, int period_ns, bool force_period)
  259. {
  260. struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip);
  261. struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
  262. u32 tin_ns = chan->tin_ns, tcnt, tcmp, oldtcmp;
  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 int pwm_samsung_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  345. const struct pwm_state *state)
  346. {
  347. int err, enabled = pwm->state.enabled;
  348. if (state->polarity != pwm->state.polarity) {
  349. if (enabled) {
  350. pwm_samsung_disable(chip, pwm);
  351. enabled = false;
  352. }
  353. err = pwm_samsung_set_polarity(chip, pwm, state->polarity);
  354. if (err)
  355. return err;
  356. }
  357. if (!state->enabled) {
  358. if (enabled)
  359. pwm_samsung_disable(chip, pwm);
  360. return 0;
  361. }
  362. /*
  363. * We currently avoid using 64bit arithmetic by using the
  364. * fact that anything faster than 1Hz is easily representable
  365. * by 32bits.
  366. */
  367. if (state->period > NSEC_PER_SEC)
  368. return -ERANGE;
  369. err = pwm_samsung_config(chip, pwm, state->duty_cycle, state->period);
  370. if (err)
  371. return err;
  372. if (!pwm->state.enabled)
  373. err = pwm_samsung_enable(chip, pwm);
  374. return err;
  375. }
  376. static const struct pwm_ops pwm_samsung_ops = {
  377. .request = pwm_samsung_request,
  378. .free = pwm_samsung_free,
  379. .apply = pwm_samsung_apply,
  380. .owner = THIS_MODULE,
  381. };
  382. #ifdef CONFIG_OF
  383. static const struct samsung_pwm_variant s3c24xx_variant = {
  384. .bits = 16,
  385. .div_base = 1,
  386. .has_tint_cstat = false,
  387. .tclk_mask = BIT(4),
  388. };
  389. static const struct samsung_pwm_variant s3c64xx_variant = {
  390. .bits = 32,
  391. .div_base = 0,
  392. .has_tint_cstat = true,
  393. .tclk_mask = BIT(7) | BIT(6) | BIT(5),
  394. };
  395. static const struct samsung_pwm_variant s5p64x0_variant = {
  396. .bits = 32,
  397. .div_base = 0,
  398. .has_tint_cstat = true,
  399. .tclk_mask = 0,
  400. };
  401. static const struct samsung_pwm_variant s5pc100_variant = {
  402. .bits = 32,
  403. .div_base = 0,
  404. .has_tint_cstat = true,
  405. .tclk_mask = BIT(5),
  406. };
  407. static const struct of_device_id samsung_pwm_matches[] = {
  408. { .compatible = "samsung,s3c2410-pwm", .data = &s3c24xx_variant },
  409. { .compatible = "samsung,s3c6400-pwm", .data = &s3c64xx_variant },
  410. { .compatible = "samsung,s5p6440-pwm", .data = &s5p64x0_variant },
  411. { .compatible = "samsung,s5pc100-pwm", .data = &s5pc100_variant },
  412. { .compatible = "samsung,exynos4210-pwm", .data = &s5p64x0_variant },
  413. {},
  414. };
  415. MODULE_DEVICE_TABLE(of, samsung_pwm_matches);
  416. static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
  417. {
  418. struct device_node *np = chip->chip.dev->of_node;
  419. const struct of_device_id *match;
  420. struct property *prop;
  421. const __be32 *cur;
  422. u32 val;
  423. match = of_match_node(samsung_pwm_matches, np);
  424. if (!match)
  425. return -ENODEV;
  426. memcpy(&chip->variant, match->data, sizeof(chip->variant));
  427. of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) {
  428. if (val >= SAMSUNG_PWM_NUM) {
  429. dev_err(chip->chip.dev,
  430. "%s: invalid channel index in samsung,pwm-outputs property\n",
  431. __func__);
  432. continue;
  433. }
  434. chip->variant.output_mask |= BIT(val);
  435. }
  436. return 0;
  437. }
  438. #else
  439. static int pwm_samsung_parse_dt(struct samsung_pwm_chip *chip)
  440. {
  441. return -ENODEV;
  442. }
  443. #endif
  444. static int pwm_samsung_probe(struct platform_device *pdev)
  445. {
  446. struct device *dev = &pdev->dev;
  447. struct samsung_pwm_chip *chip;
  448. unsigned int chan;
  449. int ret;
  450. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  451. if (chip == NULL)
  452. return -ENOMEM;
  453. chip->chip.dev = &pdev->dev;
  454. chip->chip.ops = &pwm_samsung_ops;
  455. chip->chip.npwm = SAMSUNG_PWM_NUM;
  456. chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1;
  457. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  458. ret = pwm_samsung_parse_dt(chip);
  459. if (ret)
  460. return ret;
  461. } else {
  462. if (!pdev->dev.platform_data) {
  463. dev_err(&pdev->dev, "no platform data specified\n");
  464. return -EINVAL;
  465. }
  466. memcpy(&chip->variant, pdev->dev.platform_data,
  467. sizeof(chip->variant));
  468. }
  469. chip->base = devm_platform_ioremap_resource(pdev, 0);
  470. if (IS_ERR(chip->base))
  471. return PTR_ERR(chip->base);
  472. chip->base_clk = devm_clk_get(&pdev->dev, "timers");
  473. if (IS_ERR(chip->base_clk)) {
  474. dev_err(dev, "failed to get timer base clk\n");
  475. return PTR_ERR(chip->base_clk);
  476. }
  477. ret = clk_prepare_enable(chip->base_clk);
  478. if (ret < 0) {
  479. dev_err(dev, "failed to enable base clock\n");
  480. return ret;
  481. }
  482. for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan)
  483. if (chip->variant.output_mask & BIT(chan))
  484. pwm_samsung_set_invert(chip, chan, true);
  485. /* Following clocks are optional. */
  486. chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0");
  487. chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1");
  488. platform_set_drvdata(pdev, chip);
  489. ret = pwmchip_add(&chip->chip);
  490. if (ret < 0) {
  491. dev_err(dev, "failed to register PWM chip\n");
  492. clk_disable_unprepare(chip->base_clk);
  493. return ret;
  494. }
  495. dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n",
  496. clk_get_rate(chip->base_clk),
  497. !IS_ERR(chip->tclk0) ? clk_get_rate(chip->tclk0) : 0,
  498. !IS_ERR(chip->tclk1) ? clk_get_rate(chip->tclk1) : 0);
  499. return 0;
  500. }
  501. static int pwm_samsung_remove(struct platform_device *pdev)
  502. {
  503. struct samsung_pwm_chip *chip = platform_get_drvdata(pdev);
  504. pwmchip_remove(&chip->chip);
  505. clk_disable_unprepare(chip->base_clk);
  506. return 0;
  507. }
  508. #ifdef CONFIG_PM_SLEEP
  509. static int pwm_samsung_resume(struct device *dev)
  510. {
  511. struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev);
  512. struct pwm_chip *chip = &our_chip->chip;
  513. unsigned int i;
  514. for (i = 0; i < SAMSUNG_PWM_NUM; i++) {
  515. struct pwm_device *pwm = &chip->pwms[i];
  516. struct samsung_pwm_channel *chan = pwm_get_chip_data(pwm);
  517. if (!chan)
  518. continue;
  519. if (our_chip->variant.output_mask & BIT(i))
  520. pwm_samsung_set_invert(our_chip, i,
  521. our_chip->inverter_mask & BIT(i));
  522. if (chan->period_ns) {
  523. __pwm_samsung_config(chip, pwm, chan->duty_ns,
  524. chan->period_ns, true);
  525. /* needed to make PWM disable work on Odroid-XU3 */
  526. pwm_samsung_manual_update(our_chip, pwm);
  527. }
  528. if (our_chip->disabled_mask & BIT(i))
  529. pwm_samsung_disable(chip, pwm);
  530. else
  531. pwm_samsung_enable(chip, pwm);
  532. }
  533. return 0;
  534. }
  535. #endif
  536. static SIMPLE_DEV_PM_OPS(pwm_samsung_pm_ops, NULL, pwm_samsung_resume);
  537. static struct platform_driver pwm_samsung_driver = {
  538. .driver = {
  539. .name = "samsung-pwm",
  540. .pm = &pwm_samsung_pm_ops,
  541. .of_match_table = of_match_ptr(samsung_pwm_matches),
  542. },
  543. .probe = pwm_samsung_probe,
  544. .remove = pwm_samsung_remove,
  545. };
  546. module_platform_driver(pwm_samsung_driver);
  547. MODULE_LICENSE("GPL");
  548. MODULE_AUTHOR("Tomasz Figa <[email protected]>");
  549. MODULE_ALIAS("platform:samsung-pwm");