pwm-qcom.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. */
  6. #define pr_fmt(fmt) "%s: " fmt, __func__
  7. #include <linux/clk.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pwm.h>
  14. #include <linux/regmap.h>
  15. #include <linux/debugfs.h>
  16. #define PERIOD_TO_HZ(period_ns) ((1 * 1000000000UL) / period_ns)
  17. #define FRAME_NUM_MAX_LEN 9
  18. /* Offsets */
  19. #define PWM_TOPCTL0 0x0
  20. /* offsets per frame */
  21. #define PWM_CTL0 0x0
  22. #define PWM_CTL1 0x4
  23. #define PWM_CTL2 0x8
  24. #define PWM_CYC_CFG 0xC
  25. #define PWM_UPDATE 0x10
  26. #define PWM_PERIOD_CNT 0x14
  27. #define PWM_FRAME_POLARITY_BIT 0
  28. enum {
  29. ENABLE_STATUS0,
  30. ENABLE_STATUS1,
  31. ENABLE_STATUS_REG_SIZE,
  32. };
  33. struct pdm_pwm_priv_data {
  34. unsigned int max_channels;
  35. const u16 *status_reg_offsets;
  36. };
  37. /*
  38. *struct pdm_pwm_frames - Information regarding per pdm frame
  39. * @frame_id: Id number associated with each frame.
  40. * @polarity: Current polarity of the particular frame.
  41. * @reg_offset: offset of each frame from base pdm.
  42. * @current_period_ns: Current period of the particular frame.
  43. * @current_duty_ns: Current duty cycle of the particular frame.
  44. * @current_freq: Current frequency of frame.
  45. * @freq_set: This bool flag is responsible for setting period once per frame.
  46. * @mutex: mutex lock per frame.
  47. */
  48. struct pdm_pwm_frames {
  49. u32 frame_id;
  50. u32 polarity;
  51. u32 reg_offset;
  52. u64 current_period_ns;
  53. u64 current_duty_ns;
  54. unsigned long current_freq;
  55. bool is_enabled;
  56. bool freq_set;
  57. struct mutex frame_lock; /* PWM per frame lock */
  58. struct pdm_pwm_chip *pwm_chip;
  59. };
  60. /*
  61. *struct pdm_pwm_chip - Information regarding per pdm
  62. * @pwm_chip: information per pdm.
  63. * @regmap: regmap of each pdm.
  64. * @device: pdm device.
  65. * @pdm_pwm_frames: structure for all frames of each pdm.
  66. * @pdm_ahb_clk: pdm clock for enabling pdm block
  67. * @pwm_core_clk: pwm clock for enabling each pwm.
  68. * @mutex: mutex lock per frame.
  69. * @pwm_core_rate: core rate of pwm_core__clk.
  70. * @num_frames: number of frames in each pdm.
  71. */
  72. struct pdm_pwm_chip {
  73. struct pwm_chip pwm_chip;
  74. struct regmap *regmap;
  75. struct device *dev;
  76. struct pdm_pwm_frames *frames;
  77. struct clk *pdm_ahb_clk;
  78. struct clk *pwm_core_clk;
  79. struct pdm_pwm_priv_data *priv_data;
  80. /* This lock to be used for Enable/Disable as it is per PWM channel */
  81. struct mutex lock;
  82. unsigned long pwm_core_rate;
  83. u32 num_frames;
  84. };
  85. static int __pdm_pwm_calc_pwm_frequency(struct pdm_pwm_chip *chip,
  86. int period_ns, u32 hw_idx)
  87. {
  88. unsigned long cyc_cfg, freq;
  89. int ret;
  90. /* PWM client could set the period only once, due to HW limitation. */
  91. if (chip->frames[hw_idx].freq_set)
  92. return 0;
  93. freq = PERIOD_TO_HZ(period_ns);
  94. if (!freq) {
  95. pr_err("Frequency cannot be Zero\n");
  96. return -EINVAL;
  97. }
  98. if (freq > (chip->pwm_core_rate >> 1) || freq <= (chip->pwm_core_rate >> 16)) {
  99. pr_debug("Freq %ld is not in range Max=%ld Min=%ld\n", freq,
  100. (chip->pwm_core_rate >> 1), (chip->pwm_core_rate >> 16) + 1);
  101. return -ERANGE;
  102. }
  103. cyc_cfg = DIV_ROUND_CLOSEST(chip->pwm_core_rate, freq) - 1;
  104. ret = regmap_update_bits(chip->regmap,
  105. chip->frames[hw_idx].reg_offset + PWM_CYC_CFG,
  106. GENMASK(15, 0), cyc_cfg);
  107. if (ret)
  108. return ret;
  109. chip->frames[hw_idx].current_freq = freq;
  110. chip->frames[hw_idx].freq_set = true;
  111. chip->frames[hw_idx].current_period_ns = period_ns;
  112. return 0;
  113. }
  114. static int pdm_pwm_get_state(struct pwm_chip *pwm_chip, struct pwm_device *pwm,
  115. struct pwm_state *state)
  116. {
  117. struct pdm_pwm_chip *chip = container_of(pwm_chip,
  118. struct pdm_pwm_chip, pwm_chip);
  119. state->enabled = chip->frames[pwm->hwpwm].is_enabled;
  120. state->polarity = chip->frames[pwm->hwpwm].polarity;
  121. state->period = chip->frames[pwm->hwpwm].current_period_ns;
  122. state->duty_cycle = chip->frames[pwm->hwpwm].current_duty_ns;
  123. return 0;
  124. }
  125. static int pdm_pwm_config(struct pdm_pwm_chip *chip, u32 hw_idx,
  126. int duty_ns, int period_ns, int polarity)
  127. {
  128. unsigned long ctl1;
  129. int current_period = period_ns, ret;
  130. u32 cyc_cfg;
  131. /*
  132. * 1. Enable GCC_PDM_AHB_CBCR clock for PDM block Access
  133. * 2. pwm_core_rate = clk_get_rate(pwm_core_clk); for now it is
  134. * 19.2MHz.
  135. * 3. min_freq = pwm_core_rate/2 ^ 16;
  136. * 4. max_freq = pwm_core_rate/2;
  137. * 5. calculate the frequency based on the period_ns and compare.
  138. */
  139. ret = clk_prepare_enable(chip->pdm_ahb_clk);
  140. if (ret)
  141. return ret;
  142. ret = clk_prepare_enable(chip->pwm_core_clk);
  143. if (ret)
  144. goto fail;
  145. mutex_lock(&chip->frames[hw_idx].frame_lock);
  146. ret = __pdm_pwm_calc_pwm_frequency(chip, current_period, hw_idx);
  147. if (ret)
  148. goto out;
  149. if (chip->frames[hw_idx].current_period_ns != period_ns) {
  150. pr_err("Period cannot be updated, calculating dutycycle on old period\n");
  151. current_period = chip->frames[hw_idx].current_period_ns;
  152. }
  153. if (chip->frames[hw_idx].polarity != polarity) {
  154. regmap_update_bits(chip->regmap, chip->frames[hw_idx].reg_offset
  155. + PWM_CTL0, BIT(PWM_FRAME_POLARITY_BIT), polarity);
  156. chip->frames[hw_idx].polarity = polarity;
  157. }
  158. ctl1 = DIV_ROUND_CLOSEST(chip->pwm_core_rate, chip->frames[hw_idx].current_freq);
  159. ctl1 = DIV_ROUND_CLOSEST(ctl1 * (DIV_ROUND_CLOSEST((duty_ns * 100),
  160. current_period)), 100);
  161. regmap_read(chip->regmap, chip->frames[hw_idx].reg_offset
  162. + PWM_CYC_CFG, &cyc_cfg);
  163. if ((ctl1 > cyc_cfg || ctl1 <= 0) && duty_ns != 0) {
  164. pr_err("Duty cycle cannot be set at and beyond/below this limit\n");
  165. goto out;
  166. }
  167. ret = regmap_update_bits(chip->regmap, chip->frames[hw_idx].reg_offset
  168. + PWM_CTL2, GENMASK(15, 0), 0);
  169. if (ret)
  170. goto out;
  171. ret = regmap_update_bits(chip->regmap, chip->frames[hw_idx].reg_offset
  172. + PWM_CTL1, GENMASK(15, 0), ctl1);
  173. if (ret)
  174. goto out;
  175. ret = regmap_update_bits(chip->regmap, chip->frames[hw_idx].reg_offset
  176. + PWM_UPDATE, BIT(0), 1);
  177. if (ret)
  178. goto out;
  179. chip->frames[hw_idx].current_duty_ns = duty_ns;
  180. out:
  181. mutex_unlock(&chip->frames[hw_idx].frame_lock);
  182. clk_disable_unprepare(chip->pwm_core_clk);
  183. fail:
  184. clk_disable_unprepare(chip->pdm_ahb_clk);
  185. return ret;
  186. }
  187. static void pdm_pwm_free(struct pwm_chip *pwm_chip, struct pwm_device *pwm)
  188. {
  189. struct pdm_pwm_chip *chip = container_of(pwm_chip,
  190. struct pdm_pwm_chip, pwm_chip);
  191. u32 hw_idx = pwm->hwpwm;
  192. mutex_lock(&chip->lock);
  193. chip->frames[hw_idx].freq_set = false;
  194. chip->frames[hw_idx].current_period_ns = 0;
  195. chip->frames[hw_idx].current_duty_ns = 0;
  196. mutex_unlock(&chip->lock);
  197. }
  198. static int pdm_pwm_enable(struct pdm_pwm_chip *chip, struct pwm_device *pwm)
  199. {
  200. u32 ret, val;
  201. u32 hw_idx = pwm->hwpwm;
  202. ret = clk_prepare_enable(chip->pdm_ahb_clk);
  203. if (ret)
  204. return ret;
  205. ret = clk_prepare_enable(chip->pwm_core_clk);
  206. if (ret) {
  207. clk_disable_unprepare(chip->pdm_ahb_clk);
  208. return ret;
  209. }
  210. mutex_lock(&chip->lock);
  211. /* Check the channel in Chip channel and enable the BIT in PWM_TOP */
  212. pr_debug("%s: PWM device Label %s, HW index %u, PWM index %u\n", __func__
  213. , pwm->label, hw_idx, pwm->pwm);
  214. pr_debug("%s: PWM frame-index %d, frame-offset 0x%x\n", __func__,
  215. chip->frames[hw_idx].frame_id,
  216. chip->frames[hw_idx].reg_offset);
  217. val = BIT(chip->frames[hw_idx].frame_id);
  218. ret = regmap_update_bits(chip->regmap, PWM_TOPCTL0, val, val);
  219. mutex_unlock(&chip->lock);
  220. if (ret)
  221. return ret;
  222. chip->frames[hw_idx].is_enabled = true;
  223. return 0;
  224. }
  225. static int pdm_pwm_disable(struct pdm_pwm_chip *chip, struct pwm_device *pwm)
  226. {
  227. u32 val, hw_idx = pwm->hwpwm;
  228. int ret;
  229. mutex_lock(&chip->lock);
  230. /* Check the channel in the chip and disable the BIT in PWM_TOP */
  231. pr_debug("%s:PWM device Label %s\n", __func__, pwm->label);
  232. val = BIT(chip->frames[hw_idx].frame_id);
  233. ret = regmap_update_bits(chip->regmap, PWM_TOPCTL0, val, 0);
  234. mutex_unlock(&chip->lock);
  235. if (ret)
  236. return ret;
  237. chip->frames[hw_idx].is_enabled = false;
  238. clk_disable_unprepare(chip->pwm_core_clk);
  239. clk_disable_unprepare(chip->pdm_ahb_clk);
  240. return 0;
  241. }
  242. static int pdm_pwm_apply(struct pwm_chip *pwm_chip, struct pwm_device *pwm,
  243. const struct pwm_state *state)
  244. {
  245. struct pdm_pwm_chip *chip = container_of(pwm_chip, struct pdm_pwm_chip, pwm_chip);
  246. struct pwm_state curr_state;
  247. int ret;
  248. pwm_get_state(pwm, &curr_state);
  249. if (state->period < curr_state.period)
  250. return -EINVAL;
  251. if (state->period != curr_state.period ||
  252. state->duty_cycle != curr_state.duty_cycle ||
  253. state->polarity != curr_state.polarity) {
  254. ret = pdm_pwm_config(chip, pwm->hwpwm, state->duty_cycle,
  255. state->period, state->polarity);
  256. if (ret) {
  257. pr_err("%s: Failed to update PWM configuration\n", __func__);
  258. return ret;
  259. }
  260. }
  261. if (state->enabled != curr_state.enabled) {
  262. if (state->enabled)
  263. return pdm_pwm_enable(chip, pwm);
  264. ret = pdm_pwm_disable(chip, pwm);
  265. if (ret)
  266. return ret;
  267. }
  268. return 0;
  269. }
  270. static const struct pwm_ops pdm_pwm_ops = {
  271. .apply = pdm_pwm_apply,
  272. .free = pdm_pwm_free,
  273. .get_state = pdm_pwm_get_state,
  274. };
  275. static const struct regmap_config pwm_regmap_config = {
  276. .reg_bits = 32,
  277. .reg_stride = 4,
  278. .val_bits = 32,
  279. .fast_io = true,
  280. };
  281. static int pdm_pwm_parse_dt(struct platform_device *pdev,
  282. struct pdm_pwm_chip *chip)
  283. {
  284. struct resource *res;
  285. struct device_node *np = pdev->dev.of_node;
  286. struct device_node *frame_node;
  287. void __iomem *base;
  288. int count, ret;
  289. chip->pdm_ahb_clk = devm_clk_get(chip->dev, "pdm_ahb_clk");
  290. if (IS_ERR(chip->pdm_ahb_clk)) {
  291. if (PTR_ERR(chip->pdm_ahb_clk) != -EPROBE_DEFER)
  292. dev_err(chip->dev, "Unable to get ahb clock handle\n");
  293. return PTR_ERR(chip->pdm_ahb_clk);
  294. }
  295. chip->pwm_core_clk = devm_clk_get(chip->dev, "pwm_core_clk");
  296. if (IS_ERR(chip->pwm_core_clk)) {
  297. if (PTR_ERR(chip->pwm_core_clk) != -EPROBE_DEFER)
  298. dev_err(chip->dev, "Unable to get core clock handle\n");
  299. return PTR_ERR(chip->pwm_core_clk);
  300. }
  301. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  302. if (res == NULL) {
  303. dev_err(chip->dev, "Failed to get reg base resource\n");
  304. return -EINVAL;
  305. }
  306. base = devm_ioremap(chip->dev, res->start, resource_size(res));
  307. if (!base)
  308. return -ENOMEM;
  309. chip->regmap = devm_regmap_init_mmio(chip->dev, base,
  310. &pwm_regmap_config);
  311. if (!chip->regmap) {
  312. dev_err(chip->dev, "Couldn't get regmap\n");
  313. return -EINVAL;
  314. }
  315. if (!of_find_property(np, "assigned-clocks", NULL)) {
  316. dev_err(chip->dev, "missing parent clock handle\n");
  317. return -ENODEV;
  318. }
  319. if (!of_find_property(np, "assigned-clock-rates", NULL)) {
  320. dev_err(chip->dev, "missing parent clock rate\n");
  321. return -ENODEV;
  322. }
  323. chip->pwm_core_rate = clk_get_rate(chip->pwm_core_clk);
  324. chip->num_frames = of_get_child_count(np);
  325. if (!chip->num_frames || chip->num_frames > chip->priv_data->max_channels) {
  326. dev_err(chip->dev, "PWM frames 0-%u are supported.\n",
  327. chip->priv_data->max_channels);
  328. return -EINVAL;
  329. }
  330. chip->frames = devm_kcalloc(chip->dev, chip->num_frames,
  331. sizeof(*chip->frames), GFP_KERNEL);
  332. if (!chip->frames)
  333. return -ENOMEM;
  334. count = 0;
  335. for_each_available_child_of_node(np, frame_node) {
  336. u32 n, off;
  337. if (of_property_read_u32(frame_node, "frame-index", &n)) {
  338. pr_err(FW_BUG "Missing frame-index.\n");
  339. of_node_put(frame_node);
  340. return -EINVAL;
  341. }
  342. chip->frames[count].frame_id = n;
  343. if (of_property_read_u32(frame_node, "frame-offset", &off)) {
  344. pr_err(FW_BUG "Missing frame-offset.\n");
  345. of_node_put(frame_node);
  346. return -EINVAL;
  347. }
  348. chip->frames[count].reg_offset = off;
  349. /* Holding a reference to the pdm chip for debug operations. */
  350. chip->frames[count].pwm_chip = chip;
  351. mutex_init(&chip->frames[count].frame_lock);
  352. count++;
  353. }
  354. ret = clk_prepare_enable(chip->pdm_ahb_clk);
  355. if (ret)
  356. return ret;
  357. ret = regmap_update_bits(chip->regmap, PWM_TOPCTL0, GENMASK(chip->num_frames, 0), 0);
  358. if (ret)
  359. return ret;
  360. clk_disable_unprepare(chip->pdm_ahb_clk);
  361. return 0;
  362. }
  363. #ifdef CONFIG_DEBUG_FS
  364. static int duty_get(void *data, u64 *val)
  365. {
  366. struct pdm_pwm_frames *frame = data;
  367. *val = DIV_ROUND_CLOSEST((frame->current_duty_ns * 100),
  368. frame->current_period_ns);
  369. return 0;
  370. }
  371. DEFINE_DEBUGFS_ATTRIBUTE(pwm_duty_fops, duty_get, NULL, "%lld\n");
  372. static int get_polarity(struct seq_file *m, void *unused)
  373. {
  374. struct pdm_pwm_frames *frame = m->private;
  375. struct pdm_pwm_chip *chip = frame->pwm_chip;
  376. u32 temp;
  377. regmap_read(chip->regmap, frame->reg_offset + PWM_CTL0, &temp);
  378. if (BIT(PWM_FRAME_POLARITY_BIT) & temp)
  379. seq_puts(m, "PWM_POLARITY_INVERSED\n");
  380. else
  381. seq_puts(m, "PWM_POLARITY_NORMAL\n");
  382. return 0;
  383. }
  384. static int print_polarity(struct inode *inode, struct file *file)
  385. {
  386. return single_open(file, get_polarity, inode->i_private);
  387. };
  388. static const struct file_operations pwm_polarity_fops = {
  389. .open = print_polarity,
  390. .read = seq_read,
  391. };
  392. static int enabled(void *data, u64 *val)
  393. {
  394. struct pdm_pwm_frames *frame = data;
  395. struct pdm_pwm_chip *chip = frame->pwm_chip;
  396. u32 temp, reg_offset;
  397. *val = 0;
  398. reg_offset = chip->priv_data->status_reg_offsets[ENABLE_STATUS0];
  399. if (chip->priv_data->status_reg_offsets[ENABLE_STATUS1] && frame->frame_id > 10)
  400. reg_offset = chip->priv_data->status_reg_offsets[ENABLE_STATUS1];
  401. regmap_read(chip->regmap, reg_offset, &temp);
  402. if (BIT((frame->frame_id % 10) + BIT(0)) & temp)
  403. *val = 1;
  404. return 0;
  405. }
  406. DEFINE_DEBUGFS_ATTRIBUTE(pwm_enable_fops, enabled, NULL, "%lld\n");
  407. static int print_hw_show(struct seq_file *m, void *unused)
  408. {
  409. struct pdm_pwm_frames *frame = m->private;
  410. u32 ctl1, ctl2, cyc_cfg, period_cnt;
  411. regmap_read(frame->pwm_chip->regmap, frame->reg_offset + PWM_CTL1, &ctl1);
  412. regmap_read(frame->pwm_chip->regmap, frame->reg_offset + PWM_CTL2, &ctl2);
  413. regmap_read(frame->pwm_chip->regmap, frame->reg_offset + PWM_CYC_CFG, &cyc_cfg);
  414. regmap_read(frame->pwm_chip->regmap, frame->reg_offset + PWM_PERIOD_CNT, &period_cnt);
  415. seq_printf(m, "PWM_CTL1 : 0x%x\nPWM_CTL2 : 0x%x\n", ctl1, ctl2);
  416. seq_printf(m, "PWM_CYC_CFG : 0x%x\nPWM_PERIOD_CNT : 0x%x\n", cyc_cfg, period_cnt);
  417. return 0;
  418. }
  419. static int print_hw_open(struct inode *inode, struct file *file)
  420. {
  421. return single_open(file, print_hw_show, inode->i_private);
  422. }
  423. static const struct file_operations pwm_list_regs_fops = {
  424. .open = print_hw_open,
  425. .read = seq_read,
  426. };
  427. static int freq_get(void *data, u64 *val)
  428. {
  429. struct pdm_pwm_frames *frame = data;
  430. *val = PERIOD_TO_HZ(frame->current_period_ns);
  431. return 0;
  432. }
  433. DEFINE_DEBUGFS_ATTRIBUTE(pwm_freq_fops, freq_get, NULL, "%lld\n");
  434. static int period_ns_get(void *data, u64 *val)
  435. {
  436. struct pdm_pwm_frames *frame = data;
  437. *val = frame->current_period_ns;
  438. return 0;
  439. }
  440. DEFINE_DEBUGFS_ATTRIBUTE(pwm_period_ns_fops, period_ns_get, NULL, "%lld\n");
  441. static int duty_ns_get(void *data, u64 *val)
  442. {
  443. struct pdm_pwm_frames *frame = data;
  444. *val = frame->current_duty_ns;
  445. return 0;
  446. }
  447. DEFINE_DEBUGFS_ATTRIBUTE(pwm_duty_ns_fops, duty_ns_get, NULL, "%lld\n");
  448. static void pdm_dwm_debug_init(struct pwm_chip *pwm_chip)
  449. {
  450. struct pdm_pwm_chip *chip = container_of(pwm_chip, struct pdm_pwm_chip, pwm_chip);
  451. struct pwm_device *pwm;
  452. static struct dentry *debugfs_base, *debugfs_frame_base;
  453. int i, hw_idx;
  454. char frame[FRAME_NUM_MAX_LEN];
  455. debugfs_base = debugfs_create_dir(chip->dev->of_node->name, NULL);
  456. if (IS_ERR_OR_NULL(debugfs_base)) {
  457. pr_err("Failed in creating debugfs directory.\n");
  458. return;
  459. }
  460. for (i = 0; i < pwm_chip->npwm; i++) {
  461. pwm = &pwm_chip->pwms[i];
  462. hw_idx = pwm->hwpwm;
  463. snprintf(frame, FRAME_NUM_MAX_LEN, "frame_%d", chip->frames[hw_idx].frame_id);
  464. debugfs_frame_base = debugfs_create_dir(frame, debugfs_base);
  465. debugfs_create_file("enabled", 0444, debugfs_frame_base,
  466. &chip->frames[hw_idx], &pwm_enable_fops);
  467. debugfs_create_file("polarity", 0444, debugfs_frame_base,
  468. &chip->frames[hw_idx], &pwm_polarity_fops);
  469. debugfs_create_file("current_duty", 0444, debugfs_frame_base,
  470. &chip->frames[hw_idx], &pwm_duty_fops);
  471. debugfs_create_file("pwm_print_regs", 0444, debugfs_frame_base,
  472. &chip->frames[hw_idx], &pwm_list_regs_fops);
  473. debugfs_create_file("current_frequency_hz", 0444, debugfs_frame_base,
  474. &chip->frames[hw_idx], &pwm_freq_fops);
  475. debugfs_create_file("current_period_ns", 0444, debugfs_frame_base,
  476. &chip->frames[hw_idx], &pwm_period_ns_fops);
  477. debugfs_create_file("current_duty_cycle_ns", 0444, debugfs_frame_base,
  478. &chip->frames[hw_idx], &pwm_duty_ns_fops);
  479. }
  480. }
  481. #endif
  482. static int pdm_pwm_probe(struct platform_device *pdev)
  483. {
  484. struct pdm_pwm_chip *chip;
  485. int rc;
  486. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  487. if (!chip)
  488. return -ENOMEM;
  489. chip->priv_data = (struct pdm_pwm_priv_data *)of_device_get_match_data(&pdev->dev);
  490. if (IS_ERR_OR_NULL(chip->priv_data))
  491. return -EINVAL;
  492. chip->dev = &pdev->dev;
  493. mutex_init(&chip->lock);
  494. rc = pdm_pwm_parse_dt(pdev, chip);
  495. if (rc < 0) {
  496. dev_err(chip->dev, "Devicetree properties parsing failed, rc=%d\n", rc);
  497. goto err_out;
  498. }
  499. dev_set_drvdata(chip->dev, chip);
  500. chip->pwm_chip.dev = chip->dev;
  501. chip->pwm_chip.base = -1;
  502. chip->pwm_chip.npwm = chip->num_frames;
  503. chip->pwm_chip.ops = &pdm_pwm_ops;
  504. chip->pwm_chip.of_xlate = of_pwm_xlate_with_flags;
  505. chip->pwm_chip.of_pwm_n_cells = 3;
  506. rc = pwmchip_add(&chip->pwm_chip);
  507. if (rc < 0) {
  508. dev_err(chip->dev, "Add pwmchip failed, rc=%d\n", rc);
  509. goto err_out;
  510. }
  511. #ifdef CONFIG_DEBUG_FS
  512. pdm_dwm_debug_init(&chip->pwm_chip);
  513. #endif
  514. dev_info(chip->dev, "pwmchip driver success.\n");
  515. return rc;
  516. err_out:
  517. mutex_destroy(&chip->lock);
  518. return rc;
  519. }
  520. static int pdm_pwm_remove(struct platform_device *pdev)
  521. {
  522. struct pdm_pwm_chip *chip = dev_get_drvdata(&pdev->dev);
  523. pwmchip_remove(&chip->pwm_chip);
  524. mutex_destroy(&chip->lock);
  525. dev_set_drvdata(chip->dev, NULL);
  526. return 0;
  527. }
  528. static struct pdm_pwm_priv_data pdm_pwm_reg_offsets = {
  529. .max_channels = 10,
  530. .status_reg_offsets = (u16 [ENABLE_STATUS_REG_SIZE]) {
  531. [ENABLE_STATUS0] = 0xc,
  532. },
  533. };
  534. static struct pdm_pwm_priv_data pdm_pwm_v2_reg_offsets = {
  535. .max_channels = 20,
  536. .status_reg_offsets = (u16 [ENABLE_STATUS_REG_SIZE]) {
  537. [ENABLE_STATUS0] = 0xc,
  538. [ENABLE_STATUS1] = 0x10,
  539. },
  540. };
  541. static const struct of_device_id pdm_pwm_of_match[] = {
  542. { .compatible = "qcom,pdm-pwm", .data = &pdm_pwm_reg_offsets },
  543. { .compatible = "qcom,pdm-pwm-v2", .data = &pdm_pwm_v2_reg_offsets },
  544. { },
  545. };
  546. static struct platform_driver pdm_pwm_driver = {
  547. .driver = {
  548. .name = "pdm-pwm",
  549. .of_match_table = pdm_pwm_of_match,
  550. },
  551. .probe = pdm_pwm_probe,
  552. .remove = pdm_pwm_remove,
  553. };
  554. module_platform_driver(pdm_pwm_driver);
  555. MODULE_DESCRIPTION("QTI PDM PWM driver");
  556. MODULE_LICENSE("GPL");
  557. MODULE_ALIAS("pwm:pdm-pwm");