int st_thermal_register()

in st/st_thermal.c [179:263]


int st_thermal_register(struct platform_device *pdev,
			const struct of_device_id *st_thermal_of_match)
{
	struct st_thermal_sensor *sensor;
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	const struct of_device_id *match;

	int polling_delay;
	int ret;

	if (!np) {
		dev_err(dev, "device tree node not found\n");
		return -EINVAL;
	}

	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
	if (!sensor)
		return -ENOMEM;

	sensor->dev = dev;

	match = of_match_device(st_thermal_of_match, dev);
	if (!(match && match->data))
		return -EINVAL;

	sensor->cdata = match->data;
	if (!sensor->cdata->ops)
		return -EINVAL;

	sensor->ops = sensor->cdata->ops;

	ret = (sensor->ops->regmap_init)(sensor);
	if (ret)
		return ret;

	ret = st_thermal_alloc_regfields(sensor);
	if (ret)
		return ret;

	sensor->clk = devm_clk_get(dev, "thermal");
	if (IS_ERR(sensor->clk)) {
		dev_err(dev, "failed to fetch clock\n");
		return PTR_ERR(sensor->clk);
	}

	if (sensor->ops->register_enable_irq) {
		ret = sensor->ops->register_enable_irq(sensor);
		if (ret)
			return ret;
	}

	ret = st_thermal_sensor_on(sensor);
	if (ret)
		return ret;

	ret = st_thermal_calibration(sensor);
	if (ret)
		goto sensor_off;

	polling_delay = sensor->ops->register_enable_irq ? 0 : 1000;

	sensor->thermal_dev =
		thermal_zone_device_register(dev_name(dev), 1, 0, sensor,
					     &st_tz_ops, NULL, 0, polling_delay);
	if (IS_ERR(sensor->thermal_dev)) {
		dev_err(dev, "failed to register thermal zone device\n");
		ret = PTR_ERR(sensor->thermal_dev);
		goto sensor_off;
	}
	ret = thermal_zone_device_enable(sensor->thermal_dev);
	if (ret)
		goto tzd_unregister;

	platform_set_drvdata(pdev, sensor);

	return 0;

tzd_unregister:
	thermal_zone_device_unregister(sensor->thermal_dev);
sensor_off:
	st_thermal_sensor_off(sensor);

	return ret;
}