pwm-meson.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. * Partly 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_CONSTANT_EN BIT(29)
  57. #define MISC_A_CONSTANT_EN BIT(28)
  58. #define MISC_B_INVERT_EN BIT(27)
  59. #define MISC_A_INVERT_EN BIT(26)
  60. #define MISC_B_EN BIT(1)
  61. #define MISC_A_EN BIT(0)
  62. #define MESON_NUM_PWMS 2
  63. #define MESON_NUM_MUX_PARENTS 4
  64. static struct meson_pwm_channel_data {
  65. u8 reg_offset;
  66. u8 clk_sel_shift;
  67. u8 clk_div_shift;
  68. u8 clk_en_shift;
  69. u32 pwm_en_mask;
  70. u32 const_en_mask;
  71. u32 inv_en_mask;
  72. } meson_pwm_per_channel_data[MESON_NUM_PWMS] = {
  73. {
  74. .reg_offset = REG_PWM_A,
  75. .clk_sel_shift = MISC_A_CLK_SEL_SHIFT,
  76. .clk_div_shift = MISC_A_CLK_DIV_SHIFT,
  77. .clk_en_shift = MISC_A_CLK_EN_SHIFT,
  78. .pwm_en_mask = MISC_A_EN,
  79. .const_en_mask = MISC_A_CONSTANT_EN,
  80. .inv_en_mask = MISC_A_INVERT_EN,
  81. },
  82. {
  83. .reg_offset = REG_PWM_B,
  84. .clk_sel_shift = MISC_B_CLK_SEL_SHIFT,
  85. .clk_div_shift = MISC_B_CLK_DIV_SHIFT,
  86. .clk_en_shift = MISC_B_CLK_EN_SHIFT,
  87. .pwm_en_mask = MISC_B_EN,
  88. .const_en_mask = MISC_B_CONSTANT_EN,
  89. .inv_en_mask = MISC_B_INVERT_EN,
  90. }
  91. };
  92. struct meson_pwm_channel {
  93. unsigned long rate;
  94. unsigned int hi;
  95. unsigned int lo;
  96. bool constant;
  97. bool inverted;
  98. struct clk_mux mux;
  99. struct clk_divider div;
  100. struct clk_gate gate;
  101. struct clk *clk;
  102. };
  103. struct meson_pwm_data {
  104. const char *const parent_names[MESON_NUM_MUX_PARENTS];
  105. int (*channels_init)(struct pwm_chip *chip);
  106. bool has_constant;
  107. bool has_polarity;
  108. };
  109. struct meson_pwm {
  110. const struct meson_pwm_data *data;
  111. struct meson_pwm_channel channels[MESON_NUM_PWMS];
  112. void __iomem *base;
  113. /*
  114. * Protects register (write) access to the REG_MISC_AB register
  115. * that is shared between the two PWMs.
  116. */
  117. spinlock_t lock;
  118. };
  119. static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
  120. {
  121. return pwmchip_get_drvdata(chip);
  122. }
  123. static int meson_pwm_request(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. struct device *dev = pwmchip_parent(chip);
  128. int err;
  129. err = clk_prepare_enable(channel->clk);
  130. if (err < 0) {
  131. dev_err(dev, "failed to enable clock %s: %d\n",
  132. __clk_get_name(channel->clk), err);
  133. return err;
  134. }
  135. return 0;
  136. }
  137. static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  138. {
  139. struct meson_pwm *meson = to_meson_pwm(chip);
  140. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  141. clk_disable_unprepare(channel->clk);
  142. }
  143. static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm,
  144. const struct pwm_state *state)
  145. {
  146. struct meson_pwm *meson = to_meson_pwm(chip);
  147. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  148. unsigned int cnt, duty_cnt;
  149. long fin_freq;
  150. u64 duty, period, freq;
  151. duty = state->duty_cycle;
  152. period = state->period;
  153. /*
  154. * Note this is wrong. The result is an output wave that isn't really
  155. * inverted and so is wrongly identified by .get_state as normal.
  156. * Fixing this needs some care however as some machines might rely on
  157. * this.
  158. */
  159. if (state->polarity == PWM_POLARITY_INVERSED && !meson->data->has_polarity)
  160. duty = period - duty;
  161. freq = div64_u64(NSEC_PER_SEC * 0xffffULL, period);
  162. if (freq > ULONG_MAX)
  163. freq = ULONG_MAX;
  164. fin_freq = clk_round_rate(channel->clk, freq);
  165. if (fin_freq <= 0) {
  166. dev_err(pwmchip_parent(chip),
  167. "invalid source clock frequency %llu\n", freq);
  168. return fin_freq ? fin_freq : -EINVAL;
  169. }
  170. dev_dbg(pwmchip_parent(chip), "fin_freq: %ld Hz\n", fin_freq);
  171. cnt = mul_u64_u64_div_u64(fin_freq, period, NSEC_PER_SEC);
  172. if (cnt > 0xffff) {
  173. dev_err(pwmchip_parent(chip), "unable to get period cnt\n");
  174. return -EINVAL;
  175. }
  176. dev_dbg(pwmchip_parent(chip), "period=%llu cnt=%u\n", period, cnt);
  177. if (duty == period) {
  178. channel->hi = cnt;
  179. channel->lo = 0;
  180. channel->constant = true;
  181. } else if (duty == 0) {
  182. channel->hi = 0;
  183. channel->lo = cnt;
  184. channel->constant = true;
  185. } else {
  186. duty_cnt = mul_u64_u64_div_u64(fin_freq, duty, NSEC_PER_SEC);
  187. dev_dbg(pwmchip_parent(chip), "duty=%llu duty_cnt=%u\n", duty, duty_cnt);
  188. channel->hi = duty_cnt;
  189. channel->lo = cnt - duty_cnt;
  190. channel->constant = false;
  191. }
  192. channel->rate = fin_freq;
  193. return 0;
  194. }
  195. static void meson_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  196. {
  197. struct meson_pwm *meson = to_meson_pwm(chip);
  198. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  199. struct meson_pwm_channel_data *channel_data;
  200. unsigned long flags;
  201. u32 value;
  202. int err;
  203. channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
  204. err = clk_set_rate(channel->clk, channel->rate);
  205. if (err)
  206. dev_err(pwmchip_parent(chip), "setting clock rate failed\n");
  207. spin_lock_irqsave(&meson->lock, flags);
  208. value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
  209. FIELD_PREP(PWM_LOW_MASK, channel->lo);
  210. writel(value, meson->base + channel_data->reg_offset);
  211. value = readl(meson->base + REG_MISC_AB);
  212. value |= channel_data->pwm_en_mask;
  213. if (meson->data->has_constant) {
  214. value &= ~channel_data->const_en_mask;
  215. if (channel->constant)
  216. value |= channel_data->const_en_mask;
  217. }
  218. if (meson->data->has_polarity) {
  219. value &= ~channel_data->inv_en_mask;
  220. if (channel->inverted)
  221. value |= channel_data->inv_en_mask;
  222. }
  223. writel(value, meson->base + REG_MISC_AB);
  224. spin_unlock_irqrestore(&meson->lock, flags);
  225. }
  226. static void meson_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  227. {
  228. struct meson_pwm *meson = to_meson_pwm(chip);
  229. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  230. struct meson_pwm_channel_data *channel_data;
  231. unsigned long flags;
  232. u32 value;
  233. channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
  234. spin_lock_irqsave(&meson->lock, flags);
  235. value = readl(meson->base + REG_MISC_AB);
  236. value &= ~channel_data->pwm_en_mask;
  237. if (meson->data->has_polarity) {
  238. value &= ~channel_data->inv_en_mask;
  239. if (channel->inverted)
  240. value |= channel_data->inv_en_mask;
  241. }
  242. writel(value, meson->base + REG_MISC_AB);
  243. spin_unlock_irqrestore(&meson->lock, flags);
  244. }
  245. static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  246. const struct pwm_state *state)
  247. {
  248. struct meson_pwm *meson = to_meson_pwm(chip);
  249. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  250. int err = 0;
  251. channel->inverted = (state->polarity == PWM_POLARITY_INVERSED);
  252. if (!state->enabled) {
  253. if (channel->inverted && !meson->data->has_polarity) {
  254. /*
  255. * Some of IP block revisions don't have an "always high"
  256. * setting which we can use for "inverted disabled".
  257. * Instead we achieve this by setting mux parent with
  258. * highest rate and minimum divider value, resulting
  259. * in the shortest possible duration for one "count"
  260. * and "period == duty_cycle". This results in a signal
  261. * which is LOW for one "count", while being HIGH for
  262. * the rest of the (so the signal is HIGH for slightly
  263. * less than 100% of the period, but this is the best
  264. * we can achieve).
  265. */
  266. channel->rate = ULONG_MAX;
  267. channel->hi = ~0;
  268. channel->lo = 0;
  269. channel->constant = true;
  270. meson_pwm_enable(chip, pwm);
  271. } else {
  272. meson_pwm_disable(chip, pwm);
  273. }
  274. } else {
  275. err = meson_pwm_calc(chip, pwm, state);
  276. if (err < 0)
  277. return err;
  278. meson_pwm_enable(chip, pwm);
  279. }
  280. return 0;
  281. }
  282. static u64 meson_pwm_cnt_to_ns(unsigned long fin_freq, u32 cnt)
  283. {
  284. return fin_freq ? div64_ul(NSEC_PER_SEC * (u64)cnt, fin_freq) : 0;
  285. }
  286. static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  287. struct pwm_state *state)
  288. {
  289. struct meson_pwm *meson = to_meson_pwm(chip);
  290. struct meson_pwm_channel_data *channel_data;
  291. unsigned long fin_freq;
  292. unsigned int hi, lo;
  293. u32 value;
  294. channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
  295. fin_freq = clk_get_rate(meson->channels[pwm->hwpwm].clk);
  296. value = readl(meson->base + REG_MISC_AB);
  297. state->enabled = value & channel_data->pwm_en_mask;
  298. if (meson->data->has_polarity && (value & channel_data->inv_en_mask))
  299. state->polarity = PWM_POLARITY_INVERSED;
  300. else
  301. state->polarity = PWM_POLARITY_NORMAL;
  302. value = readl(meson->base + channel_data->reg_offset);
  303. lo = FIELD_GET(PWM_LOW_MASK, value);
  304. hi = FIELD_GET(PWM_HIGH_MASK, value);
  305. state->period = meson_pwm_cnt_to_ns(fin_freq, lo + hi);
  306. state->duty_cycle = meson_pwm_cnt_to_ns(fin_freq, hi);
  307. return 0;
  308. }
  309. static const struct pwm_ops meson_pwm_ops = {
  310. .request = meson_pwm_request,
  311. .free = meson_pwm_free,
  312. .apply = meson_pwm_apply,
  313. .get_state = meson_pwm_get_state,
  314. };
  315. static int meson_pwm_init_clocks_meson8b(struct pwm_chip *chip,
  316. struct clk_parent_data *mux_parent_data)
  317. {
  318. struct meson_pwm *meson = to_meson_pwm(chip);
  319. struct device *dev = pwmchip_parent(chip);
  320. unsigned int i;
  321. char name[255];
  322. int err;
  323. for (i = 0; i < MESON_NUM_PWMS; i++) {
  324. struct meson_pwm_channel *channel = &meson->channels[i];
  325. struct clk_parent_data div_parent = {}, gate_parent = {};
  326. struct clk_init_data init = {};
  327. snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
  328. init.name = name;
  329. init.ops = &clk_mux_ops;
  330. init.flags = 0;
  331. init.parent_data = mux_parent_data;
  332. init.num_parents = MESON_NUM_MUX_PARENTS;
  333. channel->mux.reg = meson->base + REG_MISC_AB;
  334. channel->mux.shift =
  335. meson_pwm_per_channel_data[i].clk_sel_shift;
  336. channel->mux.mask = MISC_CLK_SEL_MASK;
  337. channel->mux.flags = 0;
  338. channel->mux.lock = &meson->lock;
  339. channel->mux.table = NULL;
  340. channel->mux.hw.init = &init;
  341. err = devm_clk_hw_register(dev, &channel->mux.hw);
  342. if (err)
  343. return dev_err_probe(dev, err,
  344. "failed to register %s\n", name);
  345. snprintf(name, sizeof(name), "%s#div%u", dev_name(dev), i);
  346. init.name = name;
  347. init.ops = &clk_divider_ops;
  348. init.flags = CLK_SET_RATE_PARENT;
  349. div_parent.index = -1;
  350. div_parent.hw = &channel->mux.hw;
  351. init.parent_data = &div_parent;
  352. init.num_parents = 1;
  353. channel->div.reg = meson->base + REG_MISC_AB;
  354. channel->div.shift = meson_pwm_per_channel_data[i].clk_div_shift;
  355. channel->div.width = MISC_CLK_DIV_WIDTH;
  356. channel->div.hw.init = &init;
  357. channel->div.flags = 0;
  358. channel->div.lock = &meson->lock;
  359. err = devm_clk_hw_register(dev, &channel->div.hw);
  360. if (err)
  361. return dev_err_probe(dev, err,
  362. "failed to register %s\n", name);
  363. snprintf(name, sizeof(name), "%s#gate%u", dev_name(dev), i);
  364. init.name = name;
  365. init.ops = &clk_gate_ops;
  366. init.flags = CLK_SET_RATE_PARENT | CLK_IGNORE_UNUSED;
  367. gate_parent.index = -1;
  368. gate_parent.hw = &channel->div.hw;
  369. init.parent_data = &gate_parent;
  370. init.num_parents = 1;
  371. channel->gate.reg = meson->base + REG_MISC_AB;
  372. channel->gate.bit_idx = meson_pwm_per_channel_data[i].clk_en_shift;
  373. channel->gate.hw.init = &init;
  374. channel->gate.flags = 0;
  375. channel->gate.lock = &meson->lock;
  376. err = devm_clk_hw_register(dev, &channel->gate.hw);
  377. if (err)
  378. return dev_err_probe(dev, err, "failed to register %s\n", name);
  379. channel->clk = devm_clk_hw_get_clk(dev, &channel->gate.hw, NULL);
  380. if (IS_ERR(channel->clk))
  381. return dev_err_probe(dev, PTR_ERR(channel->clk),
  382. "failed to register %s\n", name);
  383. }
  384. return 0;
  385. }
  386. static int meson_pwm_init_channels_meson8b_legacy(struct pwm_chip *chip)
  387. {
  388. struct clk_parent_data mux_parent_data[MESON_NUM_MUX_PARENTS] = {};
  389. struct meson_pwm *meson = to_meson_pwm(chip);
  390. int i;
  391. dev_warn_once(pwmchip_parent(chip),
  392. "using obsolete compatible, please consider updating dt\n");
  393. for (i = 0; i < MESON_NUM_MUX_PARENTS; i++) {
  394. mux_parent_data[i].index = -1;
  395. mux_parent_data[i].name = meson->data->parent_names[i];
  396. }
  397. return meson_pwm_init_clocks_meson8b(chip, mux_parent_data);
  398. }
  399. static int meson_pwm_init_channels_meson8b_v2(struct pwm_chip *chip)
  400. {
  401. struct clk_parent_data mux_parent_data[MESON_NUM_MUX_PARENTS] = {};
  402. int i;
  403. /*
  404. * NOTE: Instead of relying on the hard coded names in the driver
  405. * as the legacy version, this relies on DT to provide the list of
  406. * clocks.
  407. * For once, using input numbers actually makes more sense than names.
  408. * Also DT requires clock-names to be explicitly ordered, so there is
  409. * no point bothering with clock names in this case.
  410. */
  411. for (i = 0; i < MESON_NUM_MUX_PARENTS; i++)
  412. mux_parent_data[i].index = i;
  413. return meson_pwm_init_clocks_meson8b(chip, mux_parent_data);
  414. }
  415. static void meson_pwm_s4_put_clk(void *data)
  416. {
  417. struct clk *clk = data;
  418. clk_put(clk);
  419. }
  420. static int meson_pwm_init_channels_s4(struct pwm_chip *chip)
  421. {
  422. struct device *dev = pwmchip_parent(chip);
  423. struct device_node *np = dev->of_node;
  424. struct meson_pwm *meson = to_meson_pwm(chip);
  425. int i, ret;
  426. for (i = 0; i < MESON_NUM_PWMS; i++) {
  427. meson->channels[i].clk = of_clk_get(np, i);
  428. if (IS_ERR(meson->channels[i].clk))
  429. return dev_err_probe(dev,
  430. PTR_ERR(meson->channels[i].clk),
  431. "Failed to get clk\n");
  432. ret = devm_add_action_or_reset(dev, meson_pwm_s4_put_clk,
  433. meson->channels[i].clk);
  434. if (ret)
  435. return dev_err_probe(dev, ret,
  436. "Failed to add clk_put action\n");
  437. }
  438. return 0;
  439. }
  440. static const struct meson_pwm_data pwm_meson8b_data = {
  441. .parent_names = { "xtal", NULL, "fclk_div4", "fclk_div3" },
  442. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  443. };
  444. /*
  445. * Only the 2 first inputs of the GXBB AO PWMs are valid
  446. * The last 2 are grounded
  447. */
  448. static const struct meson_pwm_data pwm_gxbb_ao_data = {
  449. .parent_names = { "xtal", "clk81", NULL, NULL },
  450. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  451. };
  452. static const struct meson_pwm_data pwm_axg_ee_data = {
  453. .parent_names = { "xtal", "fclk_div5", "fclk_div4", "fclk_div3" },
  454. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  455. .has_constant = true,
  456. .has_polarity = true,
  457. };
  458. static const struct meson_pwm_data pwm_axg_ao_data = {
  459. .parent_names = { "xtal", "axg_ao_clk81", "fclk_div4", "fclk_div5" },
  460. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  461. .has_constant = true,
  462. .has_polarity = true,
  463. };
  464. static const struct meson_pwm_data pwm_g12a_ee_data = {
  465. .parent_names = { "xtal", NULL, "fclk_div4", "fclk_div3" },
  466. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  467. .has_constant = true,
  468. .has_polarity = true,
  469. };
  470. static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
  471. .parent_names = { "xtal", "g12a_ao_clk81", "fclk_div4", "fclk_div5" },
  472. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  473. .has_constant = true,
  474. .has_polarity = true,
  475. };
  476. static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
  477. .parent_names = { "xtal", "g12a_ao_clk81", NULL, NULL },
  478. .channels_init = meson_pwm_init_channels_meson8b_legacy,
  479. .has_constant = true,
  480. .has_polarity = true,
  481. };
  482. static const struct meson_pwm_data pwm_meson8_v2_data = {
  483. .channels_init = meson_pwm_init_channels_meson8b_v2,
  484. };
  485. static const struct meson_pwm_data pwm_meson_axg_v2_data = {
  486. .channels_init = meson_pwm_init_channels_meson8b_v2,
  487. .has_constant = true,
  488. .has_polarity = true,
  489. };
  490. static const struct meson_pwm_data pwm_s4_data = {
  491. .channels_init = meson_pwm_init_channels_s4,
  492. .has_constant = true,
  493. .has_polarity = true,
  494. };
  495. static const struct of_device_id meson_pwm_matches[] = {
  496. {
  497. .compatible = "amlogic,meson8-pwm-v2",
  498. .data = &pwm_meson8_v2_data
  499. },
  500. {
  501. .compatible = "amlogic,meson-axg-pwm-v2",
  502. .data = &pwm_meson_axg_v2_data
  503. },
  504. {
  505. .compatible = "amlogic,meson-g12-pwm-v2",
  506. .data = &pwm_meson_axg_v2_data
  507. },
  508. /* The following compatibles are obsolete */
  509. {
  510. .compatible = "amlogic,meson8b-pwm",
  511. .data = &pwm_meson8b_data
  512. },
  513. {
  514. .compatible = "amlogic,meson-gxbb-pwm",
  515. .data = &pwm_meson8b_data
  516. },
  517. {
  518. .compatible = "amlogic,meson-gxbb-ao-pwm",
  519. .data = &pwm_gxbb_ao_data
  520. },
  521. {
  522. .compatible = "amlogic,meson-axg-ee-pwm",
  523. .data = &pwm_axg_ee_data
  524. },
  525. {
  526. .compatible = "amlogic,meson-axg-ao-pwm",
  527. .data = &pwm_axg_ao_data
  528. },
  529. {
  530. .compatible = "amlogic,meson-g12a-ee-pwm",
  531. .data = &pwm_g12a_ee_data
  532. },
  533. {
  534. .compatible = "amlogic,meson-g12a-ao-pwm-ab",
  535. .data = &pwm_g12a_ao_ab_data
  536. },
  537. {
  538. .compatible = "amlogic,meson-g12a-ao-pwm-cd",
  539. .data = &pwm_g12a_ao_cd_data
  540. },
  541. {
  542. .compatible = "amlogic,meson-s4-pwm",
  543. .data = &pwm_s4_data
  544. },
  545. {},
  546. };
  547. MODULE_DEVICE_TABLE(of, meson_pwm_matches);
  548. static int meson_pwm_probe(struct platform_device *pdev)
  549. {
  550. struct pwm_chip *chip;
  551. struct meson_pwm *meson;
  552. int err;
  553. chip = devm_pwmchip_alloc(&pdev->dev, MESON_NUM_PWMS, sizeof(*meson));
  554. if (IS_ERR(chip))
  555. return PTR_ERR(chip);
  556. meson = to_meson_pwm(chip);
  557. meson->base = devm_platform_ioremap_resource(pdev, 0);
  558. if (IS_ERR(meson->base))
  559. return PTR_ERR(meson->base);
  560. spin_lock_init(&meson->lock);
  561. chip->ops = &meson_pwm_ops;
  562. meson->data = of_device_get_match_data(&pdev->dev);
  563. err = meson->data->channels_init(chip);
  564. if (err < 0)
  565. return err;
  566. err = devm_pwmchip_add(&pdev->dev, chip);
  567. if (err < 0)
  568. return dev_err_probe(&pdev->dev, err,
  569. "failed to register PWM chip\n");
  570. return 0;
  571. }
  572. static struct platform_driver meson_pwm_driver = {
  573. .driver = {
  574. .name = "meson-pwm",
  575. .of_match_table = meson_pwm_matches,
  576. },
  577. .probe = meson_pwm_probe,
  578. };
  579. module_platform_driver(meson_pwm_driver);
  580. MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
  581. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  582. MODULE_LICENSE("Dual BSD/GPL");