static int bd70528_probe()

in rtc-bd70528.c [235:338]


static int bd70528_probe(struct platform_device *pdev)
{
	struct bd70528_rtc *bd_rtc;
	const struct rtc_class_ops *rtc_ops;
	const char *irq_name;
	int ret;
	struct rtc_device *rtc;
	int irq;
	unsigned int hr;
	u8 hour_reg;
	enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;

	bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
	if (!bd_rtc)
		return -ENOMEM;

	bd_rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!bd_rtc->regmap) {
		dev_err(&pdev->dev, "No regmap\n");
		return -EINVAL;
	}

	bd_rtc->dev = &pdev->dev;
	rtc_ops = &bd71828_rtc_ops;

	switch (chip) {
	case ROHM_CHIP_TYPE_BD71815:
		irq_name = "bd71815-rtc-alm-0";
		bd_rtc->reg_time_start = BD71815_REG_RTC_START;

		/*
		 * See also BD718XX_ALM_EN_OFFSET:
		 * This works for BD71828 and BD71815 as they have same offset
		 * between ALM0 start and ALM0_MASK. If new ICs are to be
		 * added this requires proper check as ALM0_MASK is not located
		 * at the end of ALM0 block - but after all ALM blocks so if
		 * amount of ALMs differ the offset to enable/disable is likely
		 * to be incorrect and enable/disable must be given as own
		 * reg address here.
		 */
		bd_rtc->bd718xx_alm_block_start = BD71815_REG_RTC_ALM_START;
		hour_reg = BD71815_REG_HOUR;
		break;
	case ROHM_CHIP_TYPE_BD71828:
		irq_name = "bd71828-rtc-alm-0";
		bd_rtc->reg_time_start = BD71828_REG_RTC_START;
		bd_rtc->bd718xx_alm_block_start = BD71828_REG_RTC_ALM_START;
		hour_reg = BD71828_REG_RTC_HOUR;
		break;
	default:
		dev_err(&pdev->dev, "Unknown chip\n");
		return -ENOENT;
	}

	irq = platform_get_irq_byname(pdev, irq_name);

	if (irq < 0)
		return irq;

	platform_set_drvdata(pdev, bd_rtc);

	ret = regmap_read(bd_rtc->regmap, hour_reg, &hr);

	if (ret) {
		dev_err(&pdev->dev, "Failed to reag RTC clock\n");
		return ret;
	}

	if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
		struct rtc_time t;

		ret = rtc_ops->read_time(&pdev->dev, &t);

		if (!ret)
			ret = rtc_ops->set_time(&pdev->dev, &t);

		if (ret) {
			dev_err(&pdev->dev,
				"Setting 24H clock for RTC failed\n");
			return ret;
		}
	}

	device_set_wakeup_capable(&pdev->dev, true);
	device_wakeup_enable(&pdev->dev);

	rtc = devm_rtc_allocate_device(&pdev->dev);
	if (IS_ERR(rtc)) {
		dev_err(&pdev->dev, "RTC device creation failed\n");
		return PTR_ERR(rtc);
	}

	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
	rtc->range_max = RTC_TIMESTAMP_END_2099;
	rtc->ops = rtc_ops;

	/* Request alarm IRQ prior to registerig the RTC */
	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
					IRQF_ONESHOT, "bd70528-rtc", rtc);
	if (ret)
		return ret;

	return devm_rtc_register_device(rtc);
}