Lines Matching +full:jh7110 +full:- +full:pwm
1 // SPDX-License-Identifier: GPL-2.0
3 * jh7110_pwmdac.c -- StarFive JH7110 PWM-DAC driver
5 * Copyright (C) 2021-2023 StarFive Technology Co., Ltd.
115 value = jh7110_pwmdac_read_reg(dev->base, JH7110_PWMDAC_CTRL); in jh7110_pwmdac_set_enable()
121 jh7110_pwmdac_write_reg(dev->base, JH7110_PWMDAC_CTRL, value); in jh7110_pwmdac_set_enable()
128 value = jh7110_pwmdac_read_reg(dev->base, JH7110_PWMDAC_CTRL); in jh7110_pwmdac_set_shift()
129 if (dev->cfg.shift == PWMDAC_SHIFT_8) in jh7110_pwmdac_set_shift()
131 else if (dev->cfg.shift == PWMDAC_SHIFT_10) in jh7110_pwmdac_set_shift()
134 jh7110_pwmdac_write_reg(dev->base, JH7110_PWMDAC_CTRL, value); in jh7110_pwmdac_set_shift()
141 value = jh7110_pwmdac_read_reg(dev->base, JH7110_PWMDAC_CTRL); in jh7110_pwmdac_set_duty_cycle()
143 value |= (dev->cfg.duty_cycle & 0x3) << JH7110_PWMDAC_DUTY_CYCLE_SHIFT; in jh7110_pwmdac_set_duty_cycle()
145 jh7110_pwmdac_write_reg(dev->base, JH7110_PWMDAC_CTRL, value); in jh7110_pwmdac_set_duty_cycle()
152 value = jh7110_pwmdac_read_reg(dev->base, JH7110_PWMDAC_CTRL); in jh7110_pwmdac_set_cnt_n()
154 value |= ((dev->cfg.cnt_n - 1) & 0x1ff) << JH7110_PWMDAC_CNT_N_SHIFT; in jh7110_pwmdac_set_cnt_n()
156 jh7110_pwmdac_write_reg(dev->base, JH7110_PWMDAC_CTRL, value); in jh7110_pwmdac_set_cnt_n()
163 value = jh7110_pwmdac_read_reg(dev->base, JH7110_PWMDAC_CTRL); in jh7110_pwmdac_set_data_change()
164 if (dev->cfg.data_change == NO_CHANGE) in jh7110_pwmdac_set_data_change()
166 else if (dev->cfg.data_change == CHANGE) in jh7110_pwmdac_set_data_change()
169 jh7110_pwmdac_write_reg(dev->base, JH7110_PWMDAC_CTRL, value); in jh7110_pwmdac_set_data_change()
176 value = jh7110_pwmdac_read_reg(dev->base, JH7110_PWMDAC_CTRL); in jh7110_pwmdac_set_data_mode()
177 if (dev->cfg.data_mode == UNSIGNED_DATA) in jh7110_pwmdac_set_data_mode()
179 else if (dev->cfg.data_mode == INVERTER_DATA_MSB) in jh7110_pwmdac_set_data_mode()
182 jh7110_pwmdac_write_reg(dev->base, JH7110_PWMDAC_CTRL, value); in jh7110_pwmdac_set_data_mode()
189 value = jh7110_pwmdac_read_reg(dev->base, JH7110_PWMDAC_CTRL); in jh7110_pwmdac_set_data_shift()
191 value |= (dev->cfg.data_shift & 0x7) << JH7110_PWMDAC_DATA_SHIFT_SHIFT; in jh7110_pwmdac_set_data_shift()
193 jh7110_pwmdac_write_reg(dev->base, JH7110_PWMDAC_CTRL, value); in jh7110_pwmdac_set_data_shift()
217 struct snd_soc_dai_link *dai_link = rtd->dai_link; in jh7110_pwmdac_startup()
219 dai_link->trigger_stop = SND_SOC_TRIGGER_ORDER_LDC; in jh7110_pwmdac_startup()
228 struct jh7110_pwmdac_dev *dev = dev_get_drvdata(dai->dev); in jh7110_pwmdac_hw_params()
234 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_3; in jh7110_pwmdac_hw_params()
238 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_2; in jh7110_pwmdac_hw_params()
242 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_3; in jh7110_pwmdac_hw_params()
246 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_1; in jh7110_pwmdac_hw_params()
250 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_1; in jh7110_pwmdac_hw_params()
254 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_1; in jh7110_pwmdac_hw_params()
258 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_1; in jh7110_pwmdac_hw_params()
262 dev_err(dai->dev, "%d rate not supported\n", in jh7110_pwmdac_hw_params()
264 return -EINVAL; in jh7110_pwmdac_hw_params()
269 dev->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; in jh7110_pwmdac_hw_params()
272 dev->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; in jh7110_pwmdac_hw_params()
275 dev_err(dai->dev, "%d channels not supported\n", in jh7110_pwmdac_hw_params()
277 return -EINVAL; in jh7110_pwmdac_hw_params()
287 ret = clk_set_rate(dev->clks[1].clk, core_clk_rate); in jh7110_pwmdac_hw_params()
289 return dev_err_probe(dai->dev, ret, in jh7110_pwmdac_hw_params()
315 ret = -EINVAL; in jh7110_pwmdac_trigger()
327 ret = clk_bulk_prepare_enable(ARRAY_SIZE(dev->clks), dev->clks); in jh7110_pwmdac_crg_enable()
329 return dev_err_probe(dev->dev, ret, in jh7110_pwmdac_crg_enable()
332 ret = reset_control_deassert(dev->rst_apb); in jh7110_pwmdac_crg_enable()
334 dev_err(dev->dev, "failed to deassert pwmdac apb reset\n"); in jh7110_pwmdac_crg_enable()
338 clk_bulk_disable_unprepare(ARRAY_SIZE(dev->clks), dev->clks); in jh7110_pwmdac_crg_enable()
344 clk_bulk_disable_unprepare(ARRAY_SIZE(dev->clks), dev->clks); in jh7110_pwmdac_crg_enable()
351 struct jh7110_pwmdac_dev *dev = dev_get_drvdata(dai->dev); in jh7110_pwmdac_dai_probe()
353 snd_soc_dai_init_dma_data(dai, &dev->play_dma_data, NULL); in jh7110_pwmdac_dai_probe()
367 .name = "jh7110-pwmdac",
371 .name = "jh7110-pwmdac",
401 pwmdac->saved_ctrl = jh7110_pwmdac_read_reg(pwmdac->base, in jh7110_pwmdac_system_suspend()
416 jh7110_pwmdac_write_reg(pwmdac->base, JH7110_PWMDAC_CTRL, in jh7110_pwmdac_system_resume()
417 pwmdac->saved_ctrl); in jh7110_pwmdac_system_resume()
430 dev->cfg.shift = PWMDAC_SHIFT_8; in jh7110_pwmdac_init_params()
431 dev->cfg.duty_cycle = PWMDAC_CYCLE_CENTER; in jh7110_pwmdac_init_params()
432 dev->cfg.cnt_n = PWMDAC_SAMPLE_CNT_1; in jh7110_pwmdac_init_params()
433 dev->cfg.data_change = NO_CHANGE; in jh7110_pwmdac_init_params()
434 dev->cfg.data_mode = INVERTER_DATA_MSB; in jh7110_pwmdac_init_params()
435 dev->cfg.data_shift = PWMDAC_DATA_LEFT_SHIFT_BIT_0; in jh7110_pwmdac_init_params()
437 dev->play_dma_data.addr = dev->mapbase + JH7110_PWMDAC_WDATA; in jh7110_pwmdac_init_params()
438 dev->play_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; in jh7110_pwmdac_init_params()
439 dev->play_dma_data.fifo_size = 1; in jh7110_pwmdac_init_params()
440 dev->play_dma_data.maxburst = 16; in jh7110_pwmdac_init_params()
449 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); in jh7110_pwmdac_probe()
451 return -ENOMEM; in jh7110_pwmdac_probe()
453 dev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); in jh7110_pwmdac_probe()
454 if (IS_ERR(dev->base)) in jh7110_pwmdac_probe()
455 return PTR_ERR(dev->base); in jh7110_pwmdac_probe()
457 dev->mapbase = res->start; in jh7110_pwmdac_probe()
459 dev->clks[0].id = "apb"; in jh7110_pwmdac_probe()
460 dev->clks[1].id = "core"; in jh7110_pwmdac_probe()
462 ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(dev->clks), dev->clks); in jh7110_pwmdac_probe()
464 return dev_err_probe(&pdev->dev, ret, in jh7110_pwmdac_probe()
467 dev->rst_apb = devm_reset_control_get_exclusive(&pdev->dev, NULL); in jh7110_pwmdac_probe()
468 if (IS_ERR(dev->rst_apb)) in jh7110_pwmdac_probe()
469 return dev_err_probe(&pdev->dev, PTR_ERR(dev->rst_apb), in jh7110_pwmdac_probe()
474 dev->dev = &pdev->dev; in jh7110_pwmdac_probe()
475 dev_set_drvdata(&pdev->dev, dev); in jh7110_pwmdac_probe()
476 ret = devm_snd_soc_register_component(&pdev->dev, in jh7110_pwmdac_probe()
480 return dev_err_probe(&pdev->dev, ret, "failed to register dai\n"); in jh7110_pwmdac_probe()
482 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); in jh7110_pwmdac_probe()
484 return dev_err_probe(&pdev->dev, ret, "failed to register pcm\n"); in jh7110_pwmdac_probe()
486 pm_runtime_enable(dev->dev); in jh7110_pwmdac_probe()
487 if (!pm_runtime_enabled(&pdev->dev)) { in jh7110_pwmdac_probe()
488 ret = jh7110_pwmdac_runtime_resume(&pdev->dev); in jh7110_pwmdac_probe()
496 pm_runtime_disable(&pdev->dev); in jh7110_pwmdac_probe()
503 pm_runtime_disable(&pdev->dev); in jh7110_pwmdac_remove()
507 { .compatible = "starfive,jh7110-pwmdac" },
514 .name = "jh7110-pwmdac",
527 MODULE_DESCRIPTION("StarFive JH7110 PWM-DAC driver");