18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * MFD core driver for X-Powers' AC100 Audio Codec IC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * The AC100 is a highly integrated audio codec and RTC subsystem designed
68c2ecf20Sopenharmony_ci * for mobile applications. It has 3 I2S/PCM interfaces, a 2 channel DAC,
78c2ecf20Sopenharmony_ci * a 2 channel ADC with 5 inputs and a builtin mixer. The RTC subsystem has
88c2ecf20Sopenharmony_ci * 3 clock outputs.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * The audio codec and RTC parts are completely separate, sharing only the
118c2ecf20Sopenharmony_ci * host interface for access to its registers.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright (2016) Chen-Yu Tsai
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Author: Chen-Yu Tsai <wens@csie.org>
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/mfd/core.h>
218c2ecf20Sopenharmony_ci#include <linux/mfd/ac100.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/of.h>
248c2ecf20Sopenharmony_ci#include <linux/regmap.h>
258c2ecf20Sopenharmony_ci#include <linux/sunxi-rsb.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic const struct regmap_range ac100_writeable_ranges[] = {
288c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_CHIP_AUDIO_RST, AC100_I2S_SR_CTRL),
298c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_I2S1_CLK_CTRL, AC100_I2S1_MXR_GAIN),
308c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_I2S2_CLK_CTRL, AC100_I2S2_MXR_GAIN),
318c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_I2S3_CLK_CTRL, AC100_I2S3_SIG_PATH_CTRL),
328c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ADC_DIG_CTRL, AC100_ADC_VOL_CTRL),
338c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_HMIC_CTRL1, AC100_HMIC_STATUS),
348c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_DAC_DIG_CTRL, AC100_DAC_MXR_GAIN),
358c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ADC_APC_CTRL, AC100_LINEOUT_CTRL),
368c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ADC_DAP_L_CTRL, AC100_ADC_DAP_OPT),
378c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_DAC_DAP_CTRL, AC100_DAC_DAP_OPT),
388c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ADC_DAP_ENA, AC100_DAC_DAP_ENA),
398c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_SRC1_CTRL1, AC100_SRC1_CTRL2),
408c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_SRC2_CTRL1, AC100_SRC2_CTRL2),
418c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_CLK32K_ANALOG_CTRL, AC100_CLKOUT_CTRL3),
428c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_RTC_RST, AC100_RTC_UPD),
438c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ALM_INT_ENA, AC100_ALM_INT_STA),
448c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ALM_SEC, AC100_RTC_GP(15)),
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic const struct regmap_range ac100_volatile_ranges[] = {
488c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_CHIP_AUDIO_RST, AC100_PLL_CTRL2),
498c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_HMIC_STATUS, AC100_HMIC_STATUS),
508c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ADC_DAP_L_STA, AC100_ADC_DAP_L_STA),
518c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_SRC1_CTRL1, AC100_SRC1_CTRL1),
528c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_SRC1_CTRL3, AC100_SRC2_CTRL1),
538c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_SRC2_CTRL3, AC100_SRC2_CTRL4),
548c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_RTC_RST, AC100_RTC_RST),
558c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_RTC_SEC, AC100_ALM_INT_STA),
568c2ecf20Sopenharmony_ci	regmap_reg_range(AC100_ALM_SEC, AC100_ALM_UPD),
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic const struct regmap_access_table ac100_writeable_table = {
608c2ecf20Sopenharmony_ci	.yes_ranges	= ac100_writeable_ranges,
618c2ecf20Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(ac100_writeable_ranges),
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic const struct regmap_access_table ac100_volatile_table = {
658c2ecf20Sopenharmony_ci	.yes_ranges	= ac100_volatile_ranges,
668c2ecf20Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(ac100_volatile_ranges),
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic const struct regmap_config ac100_regmap_config = {
708c2ecf20Sopenharmony_ci	.reg_bits	= 8,
718c2ecf20Sopenharmony_ci	.val_bits	= 16,
728c2ecf20Sopenharmony_ci	.wr_table	= &ac100_writeable_table,
738c2ecf20Sopenharmony_ci	.volatile_table	= &ac100_volatile_table,
748c2ecf20Sopenharmony_ci	.max_register	= AC100_RTC_GP(15),
758c2ecf20Sopenharmony_ci	.cache_type	= REGCACHE_RBTREE,
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic struct mfd_cell ac100_cells[] = {
798c2ecf20Sopenharmony_ci	{
808c2ecf20Sopenharmony_ci		.name		= "ac100-codec",
818c2ecf20Sopenharmony_ci		.of_compatible	= "x-powers,ac100-codec",
828c2ecf20Sopenharmony_ci	}, {
838c2ecf20Sopenharmony_ci		.name		= "ac100-rtc",
848c2ecf20Sopenharmony_ci		.of_compatible	= "x-powers,ac100-rtc",
858c2ecf20Sopenharmony_ci	},
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int ac100_rsb_probe(struct sunxi_rsb_device *rdev)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct ac100_dev *ac100;
918c2ecf20Sopenharmony_ci	int ret;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ac100 = devm_kzalloc(&rdev->dev, sizeof(*ac100), GFP_KERNEL);
948c2ecf20Sopenharmony_ci	if (!ac100)
958c2ecf20Sopenharmony_ci		return -ENOMEM;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	ac100->dev = &rdev->dev;
988c2ecf20Sopenharmony_ci	sunxi_rsb_device_set_drvdata(rdev, ac100);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	ac100->regmap = devm_regmap_init_sunxi_rsb(rdev, &ac100_regmap_config);
1018c2ecf20Sopenharmony_ci	if (IS_ERR(ac100->regmap)) {
1028c2ecf20Sopenharmony_ci		ret = PTR_ERR(ac100->regmap);
1038c2ecf20Sopenharmony_ci		dev_err(ac100->dev, "regmap init failed: %d\n", ret);
1048c2ecf20Sopenharmony_ci		return ret;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	ret = devm_mfd_add_devices(ac100->dev, PLATFORM_DEVID_NONE, ac100_cells,
1088c2ecf20Sopenharmony_ci				   ARRAY_SIZE(ac100_cells), NULL, 0, NULL);
1098c2ecf20Sopenharmony_ci	if (ret) {
1108c2ecf20Sopenharmony_ci		dev_err(ac100->dev, "failed to add MFD devices: %d\n", ret);
1118c2ecf20Sopenharmony_ci		return ret;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic const struct of_device_id ac100_of_match[] = {
1188c2ecf20Sopenharmony_ci	{ .compatible = "x-powers,ac100" },
1198c2ecf20Sopenharmony_ci	{ },
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ac100_of_match);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic struct sunxi_rsb_driver ac100_rsb_driver = {
1248c2ecf20Sopenharmony_ci	.driver = {
1258c2ecf20Sopenharmony_ci		.name	= "ac100",
1268c2ecf20Sopenharmony_ci		.of_match_table	= of_match_ptr(ac100_of_match),
1278c2ecf20Sopenharmony_ci	},
1288c2ecf20Sopenharmony_ci	.probe	= ac100_rsb_probe,
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_cimodule_sunxi_rsb_driver(ac100_rsb_driver);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Audio codec MFD core driver for AC100");
1338c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
1348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
135