pwm-meson.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (c) 2016 BayLibre, SAS.
  8. * Author: Neil Armstrong <narmstrong@baylibre.com>
  9. * Copyright (C) 2014 Amlogic, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING.
  24. *
  25. * BSD LICENSE
  26. *
  27. * Copyright (c) 2016 BayLibre, SAS.
  28. * Author: Neil Armstrong <narmstrong@baylibre.com>
  29. * Copyright (C) 2014 Amlogic, Inc.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions
  33. * are met:
  34. *
  35. * * Redistributions of source code must retain the above copyright
  36. * notice, this list of conditions and the following disclaimer.
  37. * * Redistributions in binary form must reproduce the above copyright
  38. * notice, this list of conditions and the following disclaimer in
  39. * the documentation and/or other materials provided with the
  40. * distribution.
  41. * * Neither the name of Intel Corporation nor the names of its
  42. * contributors may be used to endorse or promote products derived
  43. * from this software without specific prior written permission.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  49. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  51. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  52. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  53. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  54. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  55. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. #include <linux/clk.h>
  58. #include <linux/clk-provider.h>
  59. #include <linux/err.h>
  60. #include <linux/io.h>
  61. #include <linux/kernel.h>
  62. #include <linux/module.h>
  63. #include <linux/of.h>
  64. #include <linux/of_device.h>
  65. #include <linux/platform_device.h>
  66. #include <linux/pwm.h>
  67. #include <linux/slab.h>
  68. #include <linux/spinlock.h>
  69. #define REG_PWM_A 0x0
  70. #define REG_PWM_B 0x4
  71. #define PWM_HIGH_SHIFT 16
  72. #define REG_MISC_AB 0x8
  73. #define MISC_B_CLK_EN BIT(23)
  74. #define MISC_A_CLK_EN BIT(15)
  75. #define MISC_CLK_DIV_MASK 0x7f
  76. #define MISC_B_CLK_DIV_SHIFT 16
  77. #define MISC_A_CLK_DIV_SHIFT 8
  78. #define MISC_B_CLK_SEL_SHIFT 6
  79. #define MISC_A_CLK_SEL_SHIFT 4
  80. #define MISC_CLK_SEL_WIDTH 2
  81. #define MISC_B_EN BIT(1)
  82. #define MISC_A_EN BIT(0)
  83. static const unsigned int mux_reg_shifts[] = {
  84. MISC_A_CLK_SEL_SHIFT,
  85. MISC_B_CLK_SEL_SHIFT
  86. };
  87. struct meson_pwm_channel {
  88. unsigned int hi;
  89. unsigned int lo;
  90. u8 pre_div;
  91. struct pwm_state state;
  92. struct clk *clk_parent;
  93. struct clk_mux mux;
  94. struct clk *clk;
  95. };
  96. struct meson_pwm_data {
  97. const char * const *parent_names;
  98. unsigned int num_parents;
  99. };
  100. struct meson_pwm {
  101. struct pwm_chip chip;
  102. const struct meson_pwm_data *data;
  103. void __iomem *base;
  104. u8 inverter_mask;
  105. /*
  106. * Protects register (write) access to the REG_MISC_AB register
  107. * that is shared between the two PWMs.
  108. */
  109. spinlock_t lock;
  110. };
  111. static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
  112. {
  113. return container_of(chip, struct meson_pwm, chip);
  114. }
  115. static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  116. {
  117. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  118. struct device *dev = chip->dev;
  119. int err;
  120. if (!channel)
  121. return -ENODEV;
  122. if (channel->clk_parent) {
  123. err = clk_set_parent(channel->clk, channel->clk_parent);
  124. if (err < 0) {
  125. dev_err(dev, "failed to set parent %s for %s: %d\n",
  126. __clk_get_name(channel->clk_parent),
  127. __clk_get_name(channel->clk), err);
  128. return err;
  129. }
  130. }
  131. err = clk_prepare_enable(channel->clk);
  132. if (err < 0) {
  133. dev_err(dev, "failed to enable clock %s: %d\n",
  134. __clk_get_name(channel->clk), err);
  135. return err;
  136. }
  137. chip->ops->get_state(chip, pwm, &channel->state);
  138. return 0;
  139. }
  140. static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  141. {
  142. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  143. if (channel)
  144. clk_disable_unprepare(channel->clk);
  145. }
  146. static int meson_pwm_calc(struct meson_pwm *meson,
  147. struct meson_pwm_channel *channel, unsigned int id,
  148. unsigned int duty, unsigned int period)
  149. {
  150. unsigned int pre_div, cnt, duty_cnt;
  151. unsigned long fin_freq = -1;
  152. u64 fin_ps;
  153. if (~(meson->inverter_mask >> id) & 0x1)
  154. duty = period - duty;
  155. if (period == channel->state.period &&
  156. duty == channel->state.duty_cycle)
  157. return 0;
  158. fin_freq = clk_get_rate(channel->clk);
  159. if (fin_freq == 0) {
  160. dev_err(meson->chip.dev, "invalid source clock frequency\n");
  161. return -EINVAL;
  162. }
  163. dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
  164. fin_ps = (u64)NSEC_PER_SEC * 1000;
  165. do_div(fin_ps, fin_freq);
  166. /* Calc pre_div with the period */
  167. for (pre_div = 0; pre_div <= MISC_CLK_DIV_MASK; pre_div++) {
  168. cnt = DIV_ROUND_CLOSEST_ULL((u64)period * 1000,
  169. fin_ps * (pre_div + 1));
  170. dev_dbg(meson->chip.dev, "fin_ps=%llu pre_div=%u cnt=%u\n",
  171. fin_ps, pre_div, cnt);
  172. if (cnt <= 0xffff)
  173. break;
  174. }
  175. if (pre_div > MISC_CLK_DIV_MASK) {
  176. dev_err(meson->chip.dev, "unable to get period pre_div\n");
  177. return -EINVAL;
  178. }
  179. dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period,
  180. pre_div, cnt);
  181. if (duty == period) {
  182. channel->pre_div = pre_div;
  183. channel->hi = cnt;
  184. channel->lo = 0;
  185. } else if (duty == 0) {
  186. channel->pre_div = pre_div;
  187. channel->hi = 0;
  188. channel->lo = cnt;
  189. } else {
  190. /* Then check is we can have the duty with the same pre_div */
  191. duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000,
  192. fin_ps * (pre_div + 1));
  193. if (duty_cnt > 0xffff) {
  194. dev_err(meson->chip.dev, "unable to get duty cycle\n");
  195. return -EINVAL;
  196. }
  197. dev_dbg(meson->chip.dev, "duty=%u pre_div=%u duty_cnt=%u\n",
  198. duty, pre_div, duty_cnt);
  199. channel->pre_div = pre_div;
  200. channel->hi = duty_cnt;
  201. channel->lo = cnt - duty_cnt;
  202. }
  203. return 0;
  204. }
  205. static void meson_pwm_enable(struct meson_pwm *meson,
  206. struct meson_pwm_channel *channel,
  207. unsigned int id)
  208. {
  209. u32 value, clk_shift, clk_enable, enable;
  210. unsigned int offset;
  211. unsigned long flags;
  212. switch (id) {
  213. case 0:
  214. clk_shift = MISC_A_CLK_DIV_SHIFT;
  215. clk_enable = MISC_A_CLK_EN;
  216. enable = MISC_A_EN;
  217. offset = REG_PWM_A;
  218. break;
  219. case 1:
  220. clk_shift = MISC_B_CLK_DIV_SHIFT;
  221. clk_enable = MISC_B_CLK_EN;
  222. enable = MISC_B_EN;
  223. offset = REG_PWM_B;
  224. break;
  225. default:
  226. return;
  227. }
  228. spin_lock_irqsave(&meson->lock, flags);
  229. value = readl(meson->base + REG_MISC_AB);
  230. value &= ~(MISC_CLK_DIV_MASK << clk_shift);
  231. value |= channel->pre_div << clk_shift;
  232. value |= clk_enable;
  233. writel(value, meson->base + REG_MISC_AB);
  234. value = (channel->hi << PWM_HIGH_SHIFT) | channel->lo;
  235. writel(value, meson->base + offset);
  236. value = readl(meson->base + REG_MISC_AB);
  237. value |= enable;
  238. writel(value, meson->base + REG_MISC_AB);
  239. spin_unlock_irqrestore(&meson->lock, flags);
  240. }
  241. static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
  242. {
  243. u32 value, enable;
  244. unsigned long flags;
  245. switch (id) {
  246. case 0:
  247. enable = MISC_A_EN;
  248. break;
  249. case 1:
  250. enable = MISC_B_EN;
  251. break;
  252. default:
  253. return;
  254. }
  255. spin_lock_irqsave(&meson->lock, flags);
  256. value = readl(meson->base + REG_MISC_AB);
  257. value &= ~enable;
  258. writel(value, meson->base + REG_MISC_AB);
  259. spin_unlock_irqrestore(&meson->lock, flags);
  260. }
  261. static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  262. struct pwm_state *state)
  263. {
  264. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  265. struct meson_pwm *meson = to_meson_pwm(chip);
  266. int err = 0;
  267. if (!state)
  268. return -EINVAL;
  269. if (!state->enabled) {
  270. meson_pwm_disable(meson, pwm->hwpwm);
  271. channel->state.enabled = false;
  272. return 0;
  273. }
  274. if (state->period != channel->state.period ||
  275. state->duty_cycle != channel->state.duty_cycle ||
  276. state->polarity != channel->state.polarity) {
  277. if (state->polarity != channel->state.polarity) {
  278. if (state->polarity == PWM_POLARITY_NORMAL)
  279. meson->inverter_mask |= BIT(pwm->hwpwm);
  280. else
  281. meson->inverter_mask &= ~BIT(pwm->hwpwm);
  282. }
  283. err = meson_pwm_calc(meson, channel, pwm->hwpwm,
  284. state->duty_cycle, state->period);
  285. if (err < 0)
  286. return err;
  287. channel->state.polarity = state->polarity;
  288. channel->state.period = state->period;
  289. channel->state.duty_cycle = state->duty_cycle;
  290. }
  291. if (state->enabled && !channel->state.enabled) {
  292. meson_pwm_enable(meson, channel, pwm->hwpwm);
  293. channel->state.enabled = true;
  294. }
  295. return 0;
  296. }
  297. static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  298. struct pwm_state *state)
  299. {
  300. struct meson_pwm *meson = to_meson_pwm(chip);
  301. u32 value, mask;
  302. if (!state)
  303. return;
  304. switch (pwm->hwpwm) {
  305. case 0:
  306. mask = MISC_A_EN;
  307. break;
  308. case 1:
  309. mask = MISC_B_EN;
  310. break;
  311. default:
  312. return;
  313. }
  314. value = readl(meson->base + REG_MISC_AB);
  315. state->enabled = (value & mask) != 0;
  316. }
  317. static const struct pwm_ops meson_pwm_ops = {
  318. .request = meson_pwm_request,
  319. .free = meson_pwm_free,
  320. .apply = meson_pwm_apply,
  321. .get_state = meson_pwm_get_state,
  322. .owner = THIS_MODULE,
  323. };
  324. static const char * const pwm_meson8b_parent_names[] = {
  325. "xtal", "vid_pll", "fclk_div4", "fclk_div3"
  326. };
  327. static const struct meson_pwm_data pwm_meson8b_data = {
  328. .parent_names = pwm_meson8b_parent_names,
  329. .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names),
  330. };
  331. static const char * const pwm_gxbb_parent_names[] = {
  332. "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
  333. };
  334. static const struct meson_pwm_data pwm_gxbb_data = {
  335. .parent_names = pwm_gxbb_parent_names,
  336. .num_parents = ARRAY_SIZE(pwm_gxbb_parent_names),
  337. };
  338. /*
  339. * Only the 2 first inputs of the GXBB AO PWMs are valid
  340. * The last 2 are grounded
  341. */
  342. static const char * const pwm_gxbb_ao_parent_names[] = {
  343. "xtal", "clk81"
  344. };
  345. static const struct meson_pwm_data pwm_gxbb_ao_data = {
  346. .parent_names = pwm_gxbb_ao_parent_names,
  347. .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names),
  348. };
  349. static const struct of_device_id meson_pwm_matches[] = {
  350. {
  351. .compatible = "amlogic,meson8b-pwm",
  352. .data = &pwm_meson8b_data
  353. },
  354. {
  355. .compatible = "amlogic,meson-gxbb-pwm",
  356. .data = &pwm_gxbb_data
  357. },
  358. {
  359. .compatible = "amlogic,meson-gxbb-ao-pwm",
  360. .data = &pwm_gxbb_ao_data
  361. },
  362. {},
  363. };
  364. MODULE_DEVICE_TABLE(of, meson_pwm_matches);
  365. static int meson_pwm_init_channels(struct meson_pwm *meson,
  366. struct meson_pwm_channel *channels)
  367. {
  368. struct device *dev = meson->chip.dev;
  369. struct clk_init_data init;
  370. unsigned int i;
  371. char name[255];
  372. int err;
  373. for (i = 0; i < meson->chip.npwm; i++) {
  374. struct meson_pwm_channel *channel = &channels[i];
  375. snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
  376. init.name = name;
  377. init.ops = &clk_mux_ops;
  378. init.flags = CLK_IS_BASIC;
  379. init.parent_names = meson->data->parent_names;
  380. init.num_parents = meson->data->num_parents;
  381. channel->mux.reg = meson->base + REG_MISC_AB;
  382. channel->mux.shift = mux_reg_shifts[i];
  383. channel->mux.mask = BIT(MISC_CLK_SEL_WIDTH) - 1;
  384. channel->mux.flags = 0;
  385. channel->mux.lock = &meson->lock;
  386. channel->mux.table = NULL;
  387. channel->mux.hw.init = &init;
  388. channel->clk = devm_clk_register(dev, &channel->mux.hw);
  389. if (IS_ERR(channel->clk)) {
  390. err = PTR_ERR(channel->clk);
  391. dev_err(dev, "failed to register %s: %d\n", name, err);
  392. return err;
  393. }
  394. snprintf(name, sizeof(name), "clkin%u", i);
  395. channel->clk_parent = devm_clk_get(dev, name);
  396. if (IS_ERR(channel->clk_parent)) {
  397. err = PTR_ERR(channel->clk_parent);
  398. if (err == -EPROBE_DEFER)
  399. return err;
  400. channel->clk_parent = NULL;
  401. }
  402. }
  403. return 0;
  404. }
  405. static void meson_pwm_add_channels(struct meson_pwm *meson,
  406. struct meson_pwm_channel *channels)
  407. {
  408. unsigned int i;
  409. for (i = 0; i < meson->chip.npwm; i++)
  410. pwm_set_chip_data(&meson->chip.pwms[i], &channels[i]);
  411. }
  412. static int meson_pwm_probe(struct platform_device *pdev)
  413. {
  414. struct meson_pwm_channel *channels;
  415. struct meson_pwm *meson;
  416. struct resource *regs;
  417. int err;
  418. meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL);
  419. if (!meson)
  420. return -ENOMEM;
  421. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  422. meson->base = devm_ioremap_resource(&pdev->dev, regs);
  423. if (IS_ERR(meson->base))
  424. return PTR_ERR(meson->base);
  425. spin_lock_init(&meson->lock);
  426. meson->chip.dev = &pdev->dev;
  427. meson->chip.ops = &meson_pwm_ops;
  428. meson->chip.base = -1;
  429. meson->chip.npwm = 2;
  430. meson->chip.of_xlate = of_pwm_xlate_with_flags;
  431. meson->chip.of_pwm_n_cells = 3;
  432. meson->data = of_device_get_match_data(&pdev->dev);
  433. meson->inverter_mask = BIT(meson->chip.npwm) - 1;
  434. channels = devm_kcalloc(&pdev->dev, meson->chip.npwm, sizeof(*meson),
  435. GFP_KERNEL);
  436. if (!channels)
  437. return -ENOMEM;
  438. err = meson_pwm_init_channels(meson, channels);
  439. if (err < 0)
  440. return err;
  441. err = pwmchip_add(&meson->chip);
  442. if (err < 0) {
  443. dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err);
  444. return err;
  445. }
  446. meson_pwm_add_channels(meson, channels);
  447. platform_set_drvdata(pdev, meson);
  448. return 0;
  449. }
  450. static int meson_pwm_remove(struct platform_device *pdev)
  451. {
  452. struct meson_pwm *meson = platform_get_drvdata(pdev);
  453. return pwmchip_remove(&meson->chip);
  454. }
  455. static struct platform_driver meson_pwm_driver = {
  456. .driver = {
  457. .name = "meson-pwm",
  458. .of_match_table = meson_pwm_matches,
  459. },
  460. .probe = meson_pwm_probe,
  461. .remove = meson_pwm_remove,
  462. };
  463. module_platform_driver(meson_pwm_driver);
  464. MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
  465. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  466. MODULE_LICENSE("Dual BSD/GPL");