pwm-meson.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * PWM controller driver for Amlogic Meson SoCs.
  4. *
  5. * This PWM is only a set of Gates, Dividers and Counters:
  6. * PWM output is achieved by calculating a clock that permits calculating
  7. * two periods (low and high). The counter then has to be set to switch after
  8. * N cycles for the first half period.
  9. * The hardware has no "polarity" setting. This driver reverses the period
  10. * cycles (the low length is inverted with the high length) for
  11. * PWM_POLARITY_INVERSED. This means that .get_state cannot read the polarity
  12. * from the hardware.
  13. * Setting the duty cycle will disable and re-enable the PWM output.
  14. * Disabling the PWM stops the output immediately (without waiting for the
  15. * current period to complete first).
  16. *
  17. * The public S912 (GXM) datasheet contains some documentation for this PWM
  18. * controller starting on page 543:
  19. * https://dl.khadas.com/Hardware/VIM2/Datasheet/S912_Datasheet_V0.220170314publicversion-Wesion.pdf
  20. * An updated version of this IP block is found in S922X (G12B) SoCs. The
  21. * datasheet contains the description for this IP block revision starting at
  22. * page 1084:
  23. * https://dn.odroid.com/S922X/ODROID-N2/Datasheet/S922X_Public_Datasheet_V0.2.pdf
  24. *
  25. * Copyright (c) 2016 BayLibre, SAS.
  26. * Author: Neil Armstrong <narmstrong@baylibre.com>
  27. * Copyright (C) 2014 Amlogic, Inc.
  28. */
  29. #include <linux/bitfield.h>
  30. #include <linux/bits.h>
  31. #include <linux/clk.h>
  32. #include <linux/clk-provider.h>
  33. #include <linux/err.h>
  34. #include <linux/io.h>
  35. #include <linux/kernel.h>
  36. #include <linux/math64.h>
  37. #include <linux/module.h>
  38. #include <linux/of.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/pwm.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #define REG_PWM_A 0x0
  44. #define REG_PWM_B 0x4
  45. #define PWM_LOW_MASK GENMASK(15, 0)
  46. #define PWM_HIGH_MASK GENMASK(31, 16)
  47. #define REG_MISC_AB 0x8
  48. #define MISC_B_CLK_EN_SHIFT 23
  49. #define MISC_A_CLK_EN_SHIFT 15
  50. #define MISC_CLK_DIV_WIDTH 7
  51. #define MISC_B_CLK_DIV_SHIFT 16
  52. #define MISC_A_CLK_DIV_SHIFT 8
  53. #define MISC_B_CLK_SEL_SHIFT 6
  54. #define MISC_A_CLK_SEL_SHIFT 4
  55. #define MISC_CLK_SEL_MASK 0x3
  56. #define MISC_B_EN BIT(1)
  57. #define MISC_A_EN BIT(0)
  58. #define MESON_NUM_PWMS 2
  59. #define MESON_NUM_MUX_PARENTS 4
  60. static struct meson_pwm_channel_data {
  61. u8 reg_offset;
  62. u8 clk_sel_shift;
  63. u8 clk_div_shift;
  64. u8 clk_en_shift;
  65. u32 pwm_en_mask;
  66. } meson_pwm_per_channel_data[MESON_NUM_PWMS] = {
  67. {
  68. .reg_offset = REG_PWM_A,
  69. .clk_sel_shift = MISC_A_CLK_SEL_SHIFT,
  70. .clk_div_shift = MISC_A_CLK_DIV_SHIFT,
  71. .clk_en_shift = MISC_A_CLK_EN_SHIFT,
  72. .pwm_en_mask = MISC_A_EN,
  73. },
  74. {
  75. .reg_offset = REG_PWM_B,
  76. .clk_sel_shift = MISC_B_CLK_SEL_SHIFT,
  77. .clk_div_shift = MISC_B_CLK_DIV_SHIFT,
  78. .clk_en_shift = MISC_B_CLK_EN_SHIFT,
  79. .pwm_en_mask = MISC_B_EN,
  80. }
  81. };
  82. struct meson_pwm_channel {
  83. unsigned long rate;
  84. unsigned int hi;
  85. unsigned int lo;
  86. struct clk_mux mux;
  87. struct clk_divider div;
  88. struct clk_gate gate;
  89. struct clk *clk;
  90. };
  91. struct meson_pwm_data {
  92. const char *const parent_names[MESON_NUM_MUX_PARENTS];
  93. int (*channels_init)(struct pwm_chip *chip);
  94. };
  95. struct meson_pwm {
  96. const struct meson_pwm_data *data;
  97. struct meson_pwm_channel channels[MESON_NUM_PWMS];
  98. void __iomem *base;
  99. /*
  100. * Protects register (write) access to the REG_MISC_AB register
  101. * that is shared between the two PWMs.
  102. */
  103. spinlock_t lock;
  104. };
  105. static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
  106. {
  107. return pwmchip_get_drvdata(chip);
  108. }
  109. static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  110. {
  111. struct meson_pwm *meson = to_meson_pwm(chip);
  112. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  113. struct device *dev = pwmchip_parent(chip);
  114. int err;
  115. err = clk_prepare_enable(channel->clk);
  116. if (err < 0) {
  117. dev_err(dev, "failed to enable clock %s: %d\n",
  118. __clk_get_name(channel->clk), err);
  119. return err;
  120. }
  121. return 0;
  122. }
  123. static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  124. {
  125. struct meson_pwm *meson = to_meson_pwm(chip);
  126. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  127. clk_disable_unprepare(channel->clk);
  128. }
  129. static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
  130. const struct pwm_state *state)
  131. {
  132. struct meson_pwm *meson = to_meson_pwm(chip);
  133. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  134. unsigned int cnt, duty_cnt;
  135. long fin_freq;
  136. u64 duty, period, freq;
  137. duty = state->duty_cycle;
  138. period = state->period;
  139. /*
  140. * Note this is wrong. The result is an output wave that isn't really
  141. * inverted and so is wrongly identified by .get_state as normal.
  142. * Fixing this needs some care however as some machines might rely on
  143. * this.
  144. */
  145. if (state->polarity == PWM_POLARITY_INVERSED)
  146. duty = period - duty;
  147. freq = div64_u64(NSEC_PER_SEC * 0xffffULL, period);
  148. if (freq > ULONG_MAX)
  149. freq = ULONG_MAX;
  150. fin_freq = clk_round_rate(channel->clk, freq);
  151. if (fin_freq <= 0) {
  152. dev_err(pwmchip_parent(chip),
  153. "invalid source clock frequency %llu\n", freq);
  154. return fin_freq ? fin_freq : -EINVAL;
  155. }
  156. dev_dbg(pwmchip_parent(chip), "fin_freq: %ld Hz\n", fin_freq);
  157. cnt = mul_u64_u64_div_u64(fin_freq, period, NSEC_PER_SEC);
  158. if (cnt > 0xffff) {
  159. dev_err(pwmchip_parent(chip), "unable to get period cnt\n");
  160. return -EINVAL;
  161. }
  162. dev_dbg(pwmchip_parent(chip), "period=%llu cnt=%u\n", period, cnt);
  163. if (duty == period) {
  164. channel->hi = cnt;
  165. channel->lo = 0;
  166. } else if (duty == 0) {
  167. channel->hi = 0;
  168. channel->lo = cnt;
  169. } else {
  170. duty_cnt = mul_u64_u64_div_u64(fin_freq, duty, NSEC_PER_SEC);
  171. dev_dbg(pwmchip_parent(chip), "duty=%llu duty_cnt=%u\n", duty, duty_cnt);
  172. channel->hi = duty_cnt;
  173. channel->lo = cnt - duty_cnt;
  174. }
  175. channel->rate = fin_freq;
  176. return 0;
  177. }
  178. static void meson_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  179. {
  180. struct meson_pwm *meson = to_meson_pwm(chip);
  181. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  182. struct meson_pwm_channel_data *channel_data;
  183. unsigned long flags;
  184. u32 value;
  185. int err;
  186. channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
  187. err = clk_set_rate(channel->clk, channel->rate);
  188. if (err)
  189. dev_err(pwmchip_parent(chip), "setting clock rate failed\n");
  190. spin_lock_irqsave(&meson->lock, flags);
  191. value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
  192. FIELD_PREP(PWM_LOW_MASK, channel->lo);
  193. writel(value, meson->base + channel_data->reg_offset);
  194. value = readl(meson->base + REG_MISC_AB);
  195. value |= channel_data->pwm_en_mask;
  196. writel(value, meson->base + REG_MISC_AB);
  197. spin_unlock_irqrestore(&meson->lock, flags);
  198. }
  199. static void meson_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  200. {
  201. struct meson_pwm *meson = to_meson_pwm(chip);
  202. unsigned long flags;
  203. u32 value;
  204. spin_lock_irqsave(&meson->lock, flags);
  205. value = readl(meson->base + REG_MISC_AB);
  206. value &= ~meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask;
  207. writel(value, meson->base + REG_MISC_AB);
  208. spin_unlock_irqrestore(&meson->lock, flags);
  209. }
  210. static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  211. const struct pwm_state *state)
  212. {
  213. struct meson_pwm *meson = to_meson_pwm(chip);
  214. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  215. int err = 0;
  216. if (!state->enabled) {
  217. if (state->polarity == PWM_POLARITY_INVERSED) {
  218. /*
  219. * This IP block revision doesn't have an "always high"
  220. * setting which we can use for "inverted disabled".
  221. * Instead we achieve this by setting mux parent with
  222. * highest rate and minimum divider value, resulting
  223. * in the shortest possible duration for one "count"
  224. * and "period == duty_cycle". This results in a signal
  225. * which is LOW for one "count", while being HIGH for
  226. * the rest of the (so the signal is HIGH for slightly
  227. * less than 100% of the period, but this is the best
  228. * we can achieve).
  229. */
  230. channel->rate = ULONG_MAX;
  231. channel->hi = ~0;
  232. channel->lo = 0;
  233. meson_pwm_enable(chip, pwm);
  234. } else {
  235. meson_pwm_disable(chip, pwm);
  236. }
  237. } else {
  238. err = meson_pwm_calc(chip, pwm, state);
  239. if (err < 0)
  240. return err;
  241. meson_pwm_enable(chip, pwm);
  242. }
  243. return 0;
  244. }
  245. static u64 meson_pwm_cnt_to_ns(struct pwm_chip *chip, struct pwm_device *pwm,
  246. u32 cnt)
  247. {
  248. struct meson_pwm *meson = to_meson_pwm(chip);
  249. struct meson_pwm_channel *channel;
  250. unsigned long fin_freq;
  251. /* to_meson_pwm() can only be used after .get_state() is called */
  252. channel = &meson->channels[pwm->hwpwm];
  253. fin_freq = clk_get_rate(channel->clk);
  254. if (fin_freq == 0)
  255. return 0;
  256. return div64_ul(NSEC_PER_SEC * (u64)cnt, fin_freq);
  257. }
  258. static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  259. struct pwm_state *state)
  260. {
  261. struct meson_pwm *meson = to_meson_pwm(chip);
  262. struct meson_pwm_channel_data *channel_data;
  263. struct meson_pwm_channel *channel;
  264. u32 value;
  265. channel = &meson->channels[pwm->hwpwm];
  266. channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
  267. value = readl(meson->base + REG_MISC_AB);
  268. state->enabled = value & channel_data->pwm_en_mask;
  269. value = readl(meson->base + channel_data->reg_offset);
  270. channel->lo = FIELD_GET(PWM_LOW_MASK, value);
  271. channel->hi = FIELD_GET(PWM_HIGH_MASK, value);
  272. state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->lo + channel->hi);
  273. state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
  274. state->polarity = PWM_POLARITY_NORMAL;
  275. return 0;
  276. }
  277. static const struct pwm_ops meson_pwm_ops = {
  278. .request = meson_pwm_request,
  279. .free = meson_pwm_free,
  280. .apply = meson_pwm_apply,
  281. .get_state = meson_pwm_get_state,
  282. };
  283. static int meson_pwm_init_clocks_meson8b(struct pwm_chip *chip,
  284. struct clk_parent_data *mux_parent_data)
  285. {
  286. struct meson_pwm *meson = to_meson_pwm(chip);
  287. struct device *dev = pwmchip_parent(chip);
  288. unsigned int i;
  289. char name[255];
  290. int err;
  291. for (i = 0; i < MESON_NUM_PWMS; i++) {
  292. struct meson_pwm_channel *channel = &meson->channels[i];
  293. struct clk_parent_data div_parent = {}, gate_parent = {};
  294. struct clk_init_data init = {};
  295. snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
  296. init.name = name;
  297. init.ops = &clk_mux_ops;
  298. init.flags = 0;
  299. init.parent_data = mux_parent_data;
  300. init.num_parents = MESON_NUM_MUX_PARENTS;
  301. channel->mux.reg = meson->base + REG_MISC_AB;
  302. channel->mux.shift =
  303. meson_pwm_per_channel_data[i].clk_sel_shift;
  304. channel->mux.mask = MISC_CLK_SEL_MASK;
  305. channel->mux.flags = 0;
  306. channel->mux.lock = &meson->lock;
  307. channel->mux.table = NULL;
  308. channel->mux.hw.init = &init;
  309. err = devm_clk_hw_register(dev, &channel->mux.hw);
  310. if (err)
  311. return dev_err_probe(dev, err,
  312. "failed to register %s\n", name);
  313. snprintf(name, sizeof(name), "%s#div%u", dev_name(dev), i);
  314. init.name = name;
  315. init.ops = &clk_divider_ops;
  316. init.flags = CLK_SET_RATE_PARENT;
  317. div_parent.index = -1;
  318. div_parent.hw = &channel->mux.hw;
  319. init.parent_data = &div_parent;
  320. init.num_parents = 1;
  321. channel->div.reg = meson->base + REG_MISC_AB;
  322. channel->div.shift = meson_pwm_per_channel_data[i].clk_div_shift;
  323. channel->div.width = MISC_CLK_DIV_WIDTH;
  324. channel->div.hw.init = &init;
  325. channel->div.flags = 0;
  326. channel->div.lock = &meson->lock;
  327. err = devm_clk_hw_register(dev, &channel->div.hw);
  328. if (err)
  329. return dev_err_probe(dev, err,
  330. "failed to register %s\n", name);
  331. snprintf(name, sizeof(name), "%s#gate%u", dev_name(dev), i);
  332. init.name = name;
  333. init.ops = &clk_gate_ops;
  334. init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
  335. gate_parent.index = -1;
  336. gate_parent.hw = &channel->div.hw;
  337. init.parent_data = &gate_parent;
  338. init.num_parents = 1;
  339. channel->gate.reg = meson->base + REG_MISC_AB;
  340. channel->gate.bit_idx = meson_pwm_per_channel_data[i].clk_en_shift;
  341. channel->gate.hw.init = &init;
  342. channel->gate.flags = 0;
  343. channel->gate.lock = &meson->lock;
  344. err = devm_clk_hw_register(dev, &channel->gate.hw);
  345. if (err)
  346. return dev_err_probe(dev, err, "failed to register %s\n", name);
  347. channel->clk = devm_clk_hw_get_clk(dev, &channel->gate.hw, NULL);
  348. if (IS_ERR(channel->clk))
  349. return dev_err_probe(dev, PTR_ERR(channel->clk),
  350. "failed to register %s\n", name);
  351. }
  352. return 0;
  353. }
  354. static int meson_pwm_init_channels_meson8b_legacy(struct pwm_chip *chip)
  355. {
  356. struct clk_parent_data mux_parent_data[MESON_NUM_MUX_PARENTS] = {};
  357. struct meson_pwm *meson = to_meson_pwm(chip);
  358. int i;
  359. dev_warn_once(pwmchip_parent(chip),
  360. "using obsolete compatible, please consider updating dt\n");
  361. for (i = 0; i < MESON_NUM_MUX_PARENTS; i++) {
  362. mux_parent_data[i].index = -1;
  363. mux_parent_data[i].name = meson->data->parent_names[i];
  364. }
  365. return meson_pwm_init_clocks_meson8b(chip, mux_parent_data);
  366. }
  367. static int meson_pwm_init_channels_meson8b_v2(struct pwm_chip *chip)
  368. {
  369. struct clk_parent_data mux_parent_data[MESON_NUM_MUX_PARENTS] = {};
  370. int i;
  371. /*
  372. * NOTE: Instead of relying on the hard coded names in the driver
  373. * as the legacy version, this relies on DT to provide the list of
  374. * clocks.
  375. * For once, using input numbers actually makes more sense than names.
  376. * Also DT requires clock-names to be explicitly ordered, so there is
  377. * no point bothering with clock names in this case.
  378. */
  379. for (i = 0; i < MESON_NUM_MUX_PARENTS; i++)
  380. mux_parent_data[i].index = i;
  381. return meson_pwm_init_clocks_meson8b(chip, mux_parent_data);
  382. }
  383. static void meson_pwm_s4_put_clk(void *data)
  384. {
  385. struct clk *clk = data;
  386. clk_put(clk);
  387. }
  388. static int meson_pwm_init_channels_s4(struct pwm_chip *chip)
  389. {
  390. struct device *dev = pwmchip_parent(chip);
  391. struct device_node *np = dev->of_node;
  392. struct meson_pwm *meson = to_meson_pwm(chip);
  393. int i, ret;
  394. for (i = 0; i < MESON_NUM_PWMS; i++) {
  395. meson->channels[i].clk = of_clk_get(np, i);
  396. if (IS_ERR(meson->channels[i].clk))
  397. return dev_err_probe(dev,
  398. PTR_ERR(meson->channels[i].clk),
  399. "Failed to get clk\n");
  400. ret = devm_add_action_or_reset(dev, meson_pwm_s4_put_clk,
  401. meson->channels[i].clk);
  402. if (ret)
  403. return dev_err_probe(dev, ret,
  404. "Failed to add clk_put action\n");
  405. }
  406. return 0;
  407. }
  408. static const struct meson_pwm_data pwm_meson8b_data = {
  409. .parent_names = { "xtal", NULL, "fclk_div4", "fclk_div3" },
  410. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  411. };
  412. /*
  413. * Only the 2 first inputs of the GXBB AO PWMs are valid
  414. * The last 2 are grounded
  415. */
  416. static const struct meson_pwm_data pwm_gxbb_ao_data = {
  417. .parent_names = { "xtal", "clk81", NULL, NULL },
  418. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  419. };
  420. static const struct meson_pwm_data pwm_axg_ee_data = {
  421. .parent_names = { "xtal", "fclk_div5", "fclk_div4", "fclk_div3" },
  422. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  423. };
  424. static const struct meson_pwm_data pwm_axg_ao_data = {
  425. .parent_names = { "xtal", "axg_ao_clk81", "fclk_div4", "fclk_div5" },
  426. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  427. };
  428. static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
  429. .parent_names = { "xtal", "g12a_ao_clk81", "fclk_div4", "fclk_div5" },
  430. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  431. };
  432. static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
  433. .parent_names = { "xtal", "g12a_ao_clk81", NULL, NULL },
  434. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  435. };
  436. static const struct meson_pwm_data pwm_meson8_v2_data = {
  437. .channels_init = meson_pwm_init_channels_meson8b_v2,
  438. };
  439. static const struct meson_pwm_data pwm_s4_data = {
  440. .channels_init = meson_pwm_init_channels_s4,
  441. };
  442. static const struct of_device_id meson_pwm_matches[] = {
  443. {
  444. .compatible = "amlogic,meson8-pwm-v2",
  445. .data = &pwm_meson8_v2_data
  446. },
  447. /* The following compatibles are obsolete */
  448. {
  449. .compatible = "amlogic,meson8b-pwm",
  450. .data = &pwm_meson8b_data
  451. },
  452. {
  453. .compatible = "amlogic,meson-gxbb-pwm",
  454. .data = &pwm_meson8b_data
  455. },
  456. {
  457. .compatible = "amlogic,meson-gxbb-ao-pwm",
  458. .data = &pwm_gxbb_ao_data
  459. },
  460. {
  461. .compatible = "amlogic,meson-axg-ee-pwm",
  462. .data = &pwm_axg_ee_data
  463. },
  464. {
  465. .compatible = "amlogic,meson-axg-ao-pwm",
  466. .data = &pwm_axg_ao_data
  467. },
  468. {
  469. .compatible = "amlogic,meson-g12a-ee-pwm",
  470. .data = &pwm_meson8b_data
  471. },
  472. {
  473. .compatible = "amlogic,meson-g12a-ao-pwm-ab",
  474. .data = &pwm_g12a_ao_ab_data
  475. },
  476. {
  477. .compatible = "amlogic,meson-g12a-ao-pwm-cd",
  478. .data = &pwm_g12a_ao_cd_data
  479. },
  480. {
  481. .compatible = "amlogic,meson-s4-pwm",
  482. .data = &pwm_s4_data
  483. },
  484. {},
  485. };
  486. MODULE_DEVICE_TABLE(of, meson_pwm_matches);
  487. static int meson_pwm_probe(struct platform_device *pdev)
  488. {
  489. struct pwm_chip *chip;
  490. struct meson_pwm *meson;
  491. int err;
  492. chip = devm_pwmchip_alloc(&pdev->dev, MESON_NUM_PWMS, sizeof(*meson));
  493. if (IS_ERR(chip))
  494. return PTR_ERR(chip);
  495. meson = to_meson_pwm(chip);
  496. meson->base = devm_platform_ioremap_resource(pdev, 0);
  497. if (IS_ERR(meson->base))
  498. return PTR_ERR(meson->base);
  499. spin_lock_init(&meson->lock);
  500. chip->ops = &meson_pwm_ops;
  501. meson->data = of_device_get_match_data(&pdev->dev);
  502. err = meson->data->channels_init(chip);
  503. if (err < 0)
  504. return err;
  505. err = devm_pwmchip_add(&pdev->dev, chip);
  506. if (err < 0)
  507. return dev_err_probe(&pdev->dev, err,
  508. "failed to register PWM chip\n");
  509. return 0;
  510. }
  511. static struct platform_driver meson_pwm_driver = {
  512. .driver = {
  513. .name = "meson-pwm",
  514. .of_match_table = meson_pwm_matches,
  515. },
  516. .probe = meson_pwm_probe,
  517. };
  518. module_platform_driver(meson_pwm_driver);
  519. MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
  520. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  521. MODULE_LICENSE("Dual BSD/GPL");