Lines Matching +full:ast2600 +full:- +full:scu
1 // SPDX-License-Identifier: GPL-2.0-only
11 * Ast2600:
16 #include <linux/clk-provider.h>
179 struct regmap *scu; in aspeed_adc_set_trim_data() local
185 dev_warn(data->dev, "Couldn't find syscon node\n"); in aspeed_adc_set_trim_data()
186 return -EOPNOTSUPP; in aspeed_adc_set_trim_data()
188 scu = syscon_node_to_regmap(syscon); in aspeed_adc_set_trim_data()
190 if (IS_ERR(scu)) { in aspeed_adc_set_trim_data()
191 dev_warn(data->dev, "Failed to get syscon regmap\n"); in aspeed_adc_set_trim_data()
192 return -EOPNOTSUPP; in aspeed_adc_set_trim_data()
194 if (data->model_data->trim_locate) { in aspeed_adc_set_trim_data()
195 if (regmap_read(scu, data->model_data->trim_locate->offset, in aspeed_adc_set_trim_data()
197 dev_warn(data->dev, in aspeed_adc_set_trim_data()
203 (data->model_data->trim_locate->field)) >> in aspeed_adc_set_trim_data()
204 __ffs(data->model_data->trim_locate->field); in aspeed_adc_set_trim_data()
208 dev_dbg(data->dev, in aspeed_adc_set_trim_data()
210 trimming_val, data->model_data->trim_locate->offset, in aspeed_adc_set_trim_data()
211 data->model_data->trim_locate->field); in aspeed_adc_set_trim_data()
212 writel(trimming_val, data->base + ASPEED_REG_COMPENSATION_TRIM); in aspeed_adc_set_trim_data()
224 readl(data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_compensation()
233 * value. We can get compensating value = 0x200 - ADC read raw value. in aspeed_adc_compensation()
238 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_compensation()
250 ndelay(data->sample_period_ns); in aspeed_adc_compensation()
251 adc_raw += readw(data->base + aspeed_adc_iio_channels[0].address); in aspeed_adc_compensation()
254 data->cv = BIT(ASPEED_RESOLUTION_BITS - 1) - adc_raw; in aspeed_adc_compensation()
256 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_compensation()
257 dev_dbg(data->dev, "Compensating value = %d\n", data->cv); in aspeed_adc_compensation()
266 if (rate < data->model_data->min_sampling_rate || in aspeed_adc_set_sampling_rate()
267 rate > data->model_data->max_sampling_rate) in aspeed_adc_set_sampling_rate()
268 return -EINVAL; in aspeed_adc_set_sampling_rate()
270 clk_set_rate(data->clk_scaler->clk, rate * ASPEED_CLOCKS_PER_SAMPLE); in aspeed_adc_set_sampling_rate()
271 rate = clk_get_rate(data->clk_scaler->clk); in aspeed_adc_set_sampling_rate()
272 data->sample_period_ns = DIV_ROUND_UP_ULL( in aspeed_adc_set_sampling_rate()
274 dev_dbg(data->dev, "Adc clock = %d sample period = %d ns", rate, in aspeed_adc_set_sampling_rate()
275 data->sample_period_ns); in aspeed_adc_set_sampling_rate()
289 if (data->battery_sensing && chan->channel == 7) { in aspeed_adc_read_raw()
291 readl(data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_read_raw()
296 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_read_raw()
302 *val = readw(data->base + chan->address); in aspeed_adc_read_raw()
303 *val = (*val * data->battery_mode_gain.mult) / in aspeed_adc_read_raw()
304 data->battery_mode_gain.div; in aspeed_adc_read_raw()
307 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_read_raw()
309 *val = readw(data->base + chan->address); in aspeed_adc_read_raw()
313 if (data->battery_sensing && chan->channel == 7) in aspeed_adc_read_raw()
314 *val = (data->cv * data->battery_mode_gain.mult) / in aspeed_adc_read_raw()
315 data->battery_mode_gain.div; in aspeed_adc_read_raw()
317 *val = data->cv; in aspeed_adc_read_raw()
321 *val = data->vref_mv; in aspeed_adc_read_raw()
326 *val = clk_get_rate(data->clk_scaler->clk) / in aspeed_adc_read_raw()
331 return -EINVAL; in aspeed_adc_read_raw()
351 return -EPERM; in aspeed_adc_write_raw()
354 return -EINVAL; in aspeed_adc_write_raw()
365 return -EINVAL; in aspeed_adc_reg_access()
367 *readval = readl(data->base + reg); in aspeed_adc_reg_access()
404 priv_data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_power_down()
420 if (data->model_data->vref_fixed_mv) { in aspeed_adc_vref_config()
421 data->vref_mv = data->model_data->vref_fixed_mv; in aspeed_adc_vref_config()
425 readl(data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_vref_config()
426 data->regulator = devm_regulator_get_optional(data->dev, "vref"); in aspeed_adc_vref_config()
427 if (!IS_ERR(data->regulator)) { in aspeed_adc_vref_config()
428 ret = regulator_enable(data->regulator); in aspeed_adc_vref_config()
432 data->dev, aspeed_adc_reg_disable, data->regulator); in aspeed_adc_vref_config()
435 data->vref_mv = regulator_get_voltage(data->regulator); in aspeed_adc_vref_config()
437 data->vref_mv /= 1000; in aspeed_adc_vref_config()
438 if ((data->vref_mv >= 1550) && (data->vref_mv <= 2700)) in aspeed_adc_vref_config()
443 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_vref_config()
444 else if ((data->vref_mv >= 900) && (data->vref_mv <= 1650)) in aspeed_adc_vref_config()
449 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_vref_config()
451 dev_err(data->dev, "Regulator voltage %d not support", in aspeed_adc_vref_config()
452 data->vref_mv); in aspeed_adc_vref_config()
453 return -EOPNOTSUPP; in aspeed_adc_vref_config()
456 if (PTR_ERR(data->regulator) != -ENODEV) in aspeed_adc_vref_config()
457 return PTR_ERR(data->regulator); in aspeed_adc_vref_config()
458 data->vref_mv = 2500000; in aspeed_adc_vref_config()
459 of_property_read_u32(data->dev->of_node, in aspeed_adc_vref_config()
460 "aspeed,int-vref-microvolt", in aspeed_adc_vref_config()
461 &data->vref_mv); in aspeed_adc_vref_config()
463 data->vref_mv /= 1000; in aspeed_adc_vref_config()
464 if (data->vref_mv == 2500) in aspeed_adc_vref_config()
468 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_vref_config()
469 else if (data->vref_mv == 1200) in aspeed_adc_vref_config()
473 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_vref_config()
475 dev_err(data->dev, "Voltage %d not support", data->vref_mv); in aspeed_adc_vref_config()
476 return -EOPNOTSUPP; in aspeed_adc_vref_config()
492 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*data)); in aspeed_adc_probe()
494 return -ENOMEM; in aspeed_adc_probe()
497 data->dev = &pdev->dev; in aspeed_adc_probe()
498 data->model_data = of_device_get_match_data(&pdev->dev); in aspeed_adc_probe()
501 data->base = devm_platform_ioremap_resource(pdev, 0); in aspeed_adc_probe()
502 if (IS_ERR(data->base)) in aspeed_adc_probe()
503 return PTR_ERR(data->base); in aspeed_adc_probe()
506 spin_lock_init(&data->clk_lock); in aspeed_adc_probe()
508 of_clk_get_parent_name(pdev->dev.of_node, 0)); in aspeed_adc_probe()
509 snprintf(clk_name, ARRAY_SIZE(clk_name), "%s-fixed-div", in aspeed_adc_probe()
510 data->model_data->model_name); in aspeed_adc_probe()
511 data->fixed_div_clk = clk_hw_register_fixed_factor( in aspeed_adc_probe()
512 &pdev->dev, clk_name, clk_parent_name, 0, 1, 2); in aspeed_adc_probe()
513 if (IS_ERR(data->fixed_div_clk)) in aspeed_adc_probe()
514 return PTR_ERR(data->fixed_div_clk); in aspeed_adc_probe()
516 ret = devm_add_action_or_reset(data->dev, in aspeed_adc_probe()
518 data->fixed_div_clk); in aspeed_adc_probe()
523 if (data->model_data->need_prescaler) { in aspeed_adc_probe()
524 snprintf(clk_name, ARRAY_SIZE(clk_name), "%s-prescaler", in aspeed_adc_probe()
525 data->model_data->model_name); in aspeed_adc_probe()
526 data->clk_prescaler = devm_clk_hw_register_divider( in aspeed_adc_probe()
527 &pdev->dev, clk_name, clk_parent_name, 0, in aspeed_adc_probe()
528 data->base + ASPEED_REG_CLOCK_CONTROL, 17, 15, 0, in aspeed_adc_probe()
529 &data->clk_lock); in aspeed_adc_probe()
530 if (IS_ERR(data->clk_prescaler)) in aspeed_adc_probe()
531 return PTR_ERR(data->clk_prescaler); in aspeed_adc_probe()
540 snprintf(clk_name, ARRAY_SIZE(clk_name), "%s-scaler", in aspeed_adc_probe()
541 data->model_data->model_name); in aspeed_adc_probe()
542 data->clk_scaler = devm_clk_hw_register_divider( in aspeed_adc_probe()
543 &pdev->dev, clk_name, clk_parent_name, scaler_flags, in aspeed_adc_probe()
544 data->base + ASPEED_REG_CLOCK_CONTROL, 0, in aspeed_adc_probe()
545 data->model_data->scaler_bit_width, in aspeed_adc_probe()
546 data->model_data->need_prescaler ? CLK_DIVIDER_ONE_BASED : 0, in aspeed_adc_probe()
547 &data->clk_lock); in aspeed_adc_probe()
548 if (IS_ERR(data->clk_scaler)) in aspeed_adc_probe()
549 return PTR_ERR(data->clk_scaler); in aspeed_adc_probe()
551 data->rst = devm_reset_control_get_shared(&pdev->dev, NULL); in aspeed_adc_probe()
552 if (IS_ERR(data->rst)) { in aspeed_adc_probe()
553 dev_err(&pdev->dev, in aspeed_adc_probe()
555 return PTR_ERR(data->rst); in aspeed_adc_probe()
557 reset_control_deassert(data->rst); in aspeed_adc_probe()
559 ret = devm_add_action_or_reset(data->dev, aspeed_adc_reset_assert, in aspeed_adc_probe()
560 data->rst); in aspeed_adc_probe()
572 if (of_find_property(data->dev->of_node, "aspeed,battery-sensing", in aspeed_adc_probe()
574 if (data->model_data->bat_sense_sup) { in aspeed_adc_probe()
575 data->battery_sensing = 1; in aspeed_adc_probe()
576 if (readl(data->base + ASPEED_REG_ENGINE_CONTROL) & in aspeed_adc_probe()
578 data->battery_mode_gain.mult = 3; in aspeed_adc_probe()
579 data->battery_mode_gain.div = 1; in aspeed_adc_probe()
581 data->battery_mode_gain.mult = 3; in aspeed_adc_probe()
582 data->battery_mode_gain.div = 2; in aspeed_adc_probe()
585 dev_warn(&pdev->dev, in aspeed_adc_probe()
586 "Failed to enable battery-sensing mode\n"); in aspeed_adc_probe()
589 ret = clk_prepare_enable(data->clk_scaler->clk); in aspeed_adc_probe()
592 ret = devm_add_action_or_reset(data->dev, in aspeed_adc_probe()
594 data->clk_scaler->clk); in aspeed_adc_probe()
603 readl(data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_probe()
609 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_probe()
611 ret = devm_add_action_or_reset(data->dev, aspeed_adc_power_down, in aspeed_adc_probe()
616 if (data->model_data->wait_init_sequence) { in aspeed_adc_probe()
618 ret = readl_poll_timeout(data->base + ASPEED_REG_ENGINE_CONTROL, in aspeed_adc_probe()
631 readl(data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_probe()
634 data->base + ASPEED_REG_ENGINE_CONTROL); in aspeed_adc_probe()
636 indio_dev->name = data->model_data->model_name; in aspeed_adc_probe()
637 indio_dev->info = &aspeed_adc_iio_info; in aspeed_adc_probe()
638 indio_dev->modes = INDIO_DIRECT_MODE; in aspeed_adc_probe()
639 indio_dev->channels = data->battery_sensing ? in aspeed_adc_probe()
642 indio_dev->num_channels = data->model_data->num_channels; in aspeed_adc_probe()
644 ret = devm_iio_device_register(data->dev, indio_dev); in aspeed_adc_probe()
664 .model_name = "ast2400-adc",
674 .model_name = "ast2500-adc",
686 .model_name = "ast2600-adc0",
697 .model_name = "ast2600-adc1",
708 { .compatible = "aspeed,ast2400-adc", .data = &ast2400_model_data },
709 { .compatible = "aspeed,ast2500-adc", .data = &ast2500_model_data },
710 { .compatible = "aspeed,ast2600-adc0", .data = &ast2600_adc0_model_data },
711 { .compatible = "aspeed,ast2600-adc1", .data = &ast2600_adc1_model_data },