1f300168aSKrzysztof Kozlowski // SPDX-License-Identifier: GPL-2.0+
2f300168aSKrzysztof Kozlowski //
3f300168aSKrzysztof Kozlowski // clk-max77686.c - Clock driver for Maxim 77686/MAX77802
4f300168aSKrzysztof Kozlowski //
5f300168aSKrzysztof Kozlowski // Copyright (C) 2012 Samsung Electornics
6f300168aSKrzysztof Kozlowski // Jonghwa Lee <jonghwa3.lee@samsung.com>
773118e61SJonghwa Lee
873118e61SJonghwa Lee #include <linux/kernel.h>
973118e61SJonghwa Lee #include <linux/slab.h>
1073118e61SJonghwa Lee #include <linux/err.h>
11a0c4dfeeSPaul Gortmaker #include <linux/module.h>
1273118e61SJonghwa Lee #include <linux/platform_device.h>
135a227cd1SLaxman Dewangan #include <linux/mfd/max77620.h>
1473118e61SJonghwa Lee #include <linux/mfd/max77686.h>
1573118e61SJonghwa Lee #include <linux/mfd/max77686-private.h>
1673118e61SJonghwa Lee #include <linux/clk-provider.h>
1773118e61SJonghwa Lee #include <linux/mutex.h>
1873118e61SJonghwa Lee #include <linux/clkdev.h>
198ad313feSLaxman Dewangan #include <linux/of.h>
208ad313feSLaxman Dewangan #include <linux/regmap.h>
2173118e61SJonghwa Lee
22a8a76f56SJavier Martinez Canillas #include <dt-bindings/clock/maxim,max77686.h>
238ad313feSLaxman Dewangan #include <dt-bindings/clock/maxim,max77802.h>
245a227cd1SLaxman Dewangan #include <dt-bindings/clock/maxim,max77620.h>
2573118e61SJonghwa Lee
268ad313feSLaxman Dewangan #define MAX77802_CLOCK_LOW_JITTER_SHIFT 0x3
278ad313feSLaxman Dewangan
288ad313feSLaxman Dewangan enum max77686_chip_name {
298ad313feSLaxman Dewangan CHIP_MAX77686,
308ad313feSLaxman Dewangan CHIP_MAX77802,
315a227cd1SLaxman Dewangan CHIP_MAX77620,
328ad313feSLaxman Dewangan };
338ad313feSLaxman Dewangan
348ad313feSLaxman Dewangan struct max77686_hw_clk_info {
358ad313feSLaxman Dewangan const char *name;
368ad313feSLaxman Dewangan u32 clk_reg;
378ad313feSLaxman Dewangan u32 clk_enable_mask;
388ad313feSLaxman Dewangan u32 flags;
398ad313feSLaxman Dewangan };
408ad313feSLaxman Dewangan
418ad313feSLaxman Dewangan struct max77686_clk_init_data {
428ad313feSLaxman Dewangan struct regmap *regmap;
438ad313feSLaxman Dewangan struct clk_hw hw;
448ad313feSLaxman Dewangan struct clk_init_data clk_idata;
458ad313feSLaxman Dewangan const struct max77686_hw_clk_info *clk_info;
468ad313feSLaxman Dewangan };
478ad313feSLaxman Dewangan
488ad313feSLaxman Dewangan struct max77686_clk_driver_data {
498ad313feSLaxman Dewangan enum max77686_chip_name chip;
508ad313feSLaxman Dewangan struct max77686_clk_init_data *max_clk_data;
519b4cac33SStephen Boyd size_t num_clks;
528ad313feSLaxman Dewangan };
538ad313feSLaxman Dewangan
548ad313feSLaxman Dewangan static const struct
558ad313feSLaxman Dewangan max77686_hw_clk_info max77686_hw_clks_info[MAX77686_CLKS_NUM] = {
5673118e61SJonghwa Lee [MAX77686_CLK_AP] = {
5773118e61SJonghwa Lee .name = "32khz_ap",
588ad313feSLaxman Dewangan .clk_reg = MAX77686_REG_32KHZ,
598ad313feSLaxman Dewangan .clk_enable_mask = BIT(MAX77686_CLK_AP),
6073118e61SJonghwa Lee },
6173118e61SJonghwa Lee [MAX77686_CLK_CP] = {
6273118e61SJonghwa Lee .name = "32khz_cp",
638ad313feSLaxman Dewangan .clk_reg = MAX77686_REG_32KHZ,
648ad313feSLaxman Dewangan .clk_enable_mask = BIT(MAX77686_CLK_CP),
6573118e61SJonghwa Lee },
6673118e61SJonghwa Lee [MAX77686_CLK_PMIC] = {
6773118e61SJonghwa Lee .name = "32khz_pmic",
688ad313feSLaxman Dewangan .clk_reg = MAX77686_REG_32KHZ,
698ad313feSLaxman Dewangan .clk_enable_mask = BIT(MAX77686_CLK_PMIC),
7073118e61SJonghwa Lee },
7173118e61SJonghwa Lee };
7273118e61SJonghwa Lee
738ad313feSLaxman Dewangan static const struct
748ad313feSLaxman Dewangan max77686_hw_clk_info max77802_hw_clks_info[MAX77802_CLKS_NUM] = {
758ad313feSLaxman Dewangan [MAX77802_CLK_32K_AP] = {
768ad313feSLaxman Dewangan .name = "32khz_ap",
778ad313feSLaxman Dewangan .clk_reg = MAX77802_REG_32KHZ,
788ad313feSLaxman Dewangan .clk_enable_mask = BIT(MAX77802_CLK_32K_AP),
798ad313feSLaxman Dewangan },
808ad313feSLaxman Dewangan [MAX77802_CLK_32K_CP] = {
818ad313feSLaxman Dewangan .name = "32khz_cp",
828ad313feSLaxman Dewangan .clk_reg = MAX77802_REG_32KHZ,
838ad313feSLaxman Dewangan .clk_enable_mask = BIT(MAX77802_CLK_32K_CP),
848ad313feSLaxman Dewangan },
858ad313feSLaxman Dewangan };
868ad313feSLaxman Dewangan
875a227cd1SLaxman Dewangan static const struct
885a227cd1SLaxman Dewangan max77686_hw_clk_info max77620_hw_clks_info[MAX77620_CLKS_NUM] = {
895a227cd1SLaxman Dewangan [MAX77620_CLK_32K_OUT0] = {
905a227cd1SLaxman Dewangan .name = "32khz_out0",
915a227cd1SLaxman Dewangan .clk_reg = MAX77620_REG_CNFG1_32K,
925a227cd1SLaxman Dewangan .clk_enable_mask = MAX77620_CNFG1_32K_OUT0_EN,
935a227cd1SLaxman Dewangan },
945a227cd1SLaxman Dewangan };
955a227cd1SLaxman Dewangan
to_max77686_clk_init_data(struct clk_hw * hw)968ad313feSLaxman Dewangan static struct max77686_clk_init_data *to_max77686_clk_init_data(
978ad313feSLaxman Dewangan struct clk_hw *hw)
988ad313feSLaxman Dewangan {
998ad313feSLaxman Dewangan return container_of(hw, struct max77686_clk_init_data, hw);
1008ad313feSLaxman Dewangan }
1018ad313feSLaxman Dewangan
max77686_clk_prepare(struct clk_hw * hw)1028ad313feSLaxman Dewangan static int max77686_clk_prepare(struct clk_hw *hw)
1038ad313feSLaxman Dewangan {
1048ad313feSLaxman Dewangan struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
1058ad313feSLaxman Dewangan
1068ad313feSLaxman Dewangan return regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
1078ad313feSLaxman Dewangan max77686->clk_info->clk_enable_mask,
1088ad313feSLaxman Dewangan max77686->clk_info->clk_enable_mask);
1098ad313feSLaxman Dewangan }
1108ad313feSLaxman Dewangan
max77686_clk_unprepare(struct clk_hw * hw)1118ad313feSLaxman Dewangan static void max77686_clk_unprepare(struct clk_hw *hw)
1128ad313feSLaxman Dewangan {
1138ad313feSLaxman Dewangan struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
1148ad313feSLaxman Dewangan
1158ad313feSLaxman Dewangan regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
1168ad313feSLaxman Dewangan max77686->clk_info->clk_enable_mask,
1178ad313feSLaxman Dewangan ~max77686->clk_info->clk_enable_mask);
1188ad313feSLaxman Dewangan }
1198ad313feSLaxman Dewangan
max77686_clk_is_prepared(struct clk_hw * hw)1208ad313feSLaxman Dewangan static int max77686_clk_is_prepared(struct clk_hw *hw)
1218ad313feSLaxman Dewangan {
1228ad313feSLaxman Dewangan struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
1238ad313feSLaxman Dewangan int ret;
1248ad313feSLaxman Dewangan u32 val;
1258ad313feSLaxman Dewangan
1268ad313feSLaxman Dewangan ret = regmap_read(max77686->regmap, max77686->clk_info->clk_reg, &val);
1278ad313feSLaxman Dewangan
1288ad313feSLaxman Dewangan if (ret < 0)
1298ad313feSLaxman Dewangan return -EINVAL;
1308ad313feSLaxman Dewangan
1318ad313feSLaxman Dewangan return val & max77686->clk_info->clk_enable_mask;
1328ad313feSLaxman Dewangan }
1338ad313feSLaxman Dewangan
max77686_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)1348ad313feSLaxman Dewangan static unsigned long max77686_recalc_rate(struct clk_hw *hw,
1358ad313feSLaxman Dewangan unsigned long parent_rate)
1368ad313feSLaxman Dewangan {
1378ad313feSLaxman Dewangan return 32768;
1388ad313feSLaxman Dewangan }
1398ad313feSLaxman Dewangan
14025600dadSJulia Lawall static const struct clk_ops max77686_clk_ops = {
1418ad313feSLaxman Dewangan .prepare = max77686_clk_prepare,
1428ad313feSLaxman Dewangan .unprepare = max77686_clk_unprepare,
1438ad313feSLaxman Dewangan .is_prepared = max77686_clk_is_prepared,
1448ad313feSLaxman Dewangan .recalc_rate = max77686_recalc_rate,
1458ad313feSLaxman Dewangan };
1468ad313feSLaxman Dewangan
1479b4cac33SStephen Boyd static struct clk_hw *
of_clk_max77686_get(struct of_phandle_args * clkspec,void * data)1489b4cac33SStephen Boyd of_clk_max77686_get(struct of_phandle_args *clkspec, void *data)
1499b4cac33SStephen Boyd {
1509b4cac33SStephen Boyd struct max77686_clk_driver_data *drv_data = data;
1519b4cac33SStephen Boyd unsigned int idx = clkspec->args[0];
1529b4cac33SStephen Boyd
1539b4cac33SStephen Boyd if (idx >= drv_data->num_clks) {
1549b4cac33SStephen Boyd pr_err("%s: invalid index %u\n", __func__, idx);
1559b4cac33SStephen Boyd return ERR_PTR(-EINVAL);
1569b4cac33SStephen Boyd }
1579b4cac33SStephen Boyd
1589b4cac33SStephen Boyd return &drv_data->max_clk_data[idx].hw;
1599b4cac33SStephen Boyd }
1609b4cac33SStephen Boyd
max77686_clk_probe(struct platform_device * pdev)161018ae93fSBill Pemberton static int max77686_clk_probe(struct platform_device *pdev)
16273118e61SJonghwa Lee {
1638ad313feSLaxman Dewangan struct device *dev = &pdev->dev;
1648ad313feSLaxman Dewangan struct device *parent = dev->parent;
1658ad313feSLaxman Dewangan const struct platform_device_id *id = platform_get_device_id(pdev);
1668ad313feSLaxman Dewangan struct max77686_clk_driver_data *drv_data;
1678ad313feSLaxman Dewangan const struct max77686_hw_clk_info *hw_clks;
1688ad313feSLaxman Dewangan struct regmap *regmap;
1698ad313feSLaxman Dewangan int i, ret, num_clks;
17073118e61SJonghwa Lee
1718ad313feSLaxman Dewangan drv_data = devm_kzalloc(dev, sizeof(*drv_data), GFP_KERNEL);
1728ad313feSLaxman Dewangan if (!drv_data)
1738ad313feSLaxman Dewangan return -ENOMEM;
1748ad313feSLaxman Dewangan
1758ad313feSLaxman Dewangan regmap = dev_get_regmap(parent, NULL);
1768ad313feSLaxman Dewangan if (!regmap) {
1778ad313feSLaxman Dewangan dev_err(dev, "Failed to get rtc regmap\n");
1788ad313feSLaxman Dewangan return -ENODEV;
1798ad313feSLaxman Dewangan }
1808ad313feSLaxman Dewangan
1818ad313feSLaxman Dewangan drv_data->chip = id->driver_data;
1828ad313feSLaxman Dewangan
1838ad313feSLaxman Dewangan switch (drv_data->chip) {
1848ad313feSLaxman Dewangan case CHIP_MAX77686:
1858ad313feSLaxman Dewangan num_clks = MAX77686_CLKS_NUM;
1868ad313feSLaxman Dewangan hw_clks = max77686_hw_clks_info;
1878ad313feSLaxman Dewangan break;
1888ad313feSLaxman Dewangan
1898ad313feSLaxman Dewangan case CHIP_MAX77802:
1908ad313feSLaxman Dewangan num_clks = MAX77802_CLKS_NUM;
1918ad313feSLaxman Dewangan hw_clks = max77802_hw_clks_info;
1928ad313feSLaxman Dewangan break;
1938ad313feSLaxman Dewangan
1945a227cd1SLaxman Dewangan case CHIP_MAX77620:
1955a227cd1SLaxman Dewangan num_clks = MAX77620_CLKS_NUM;
1965a227cd1SLaxman Dewangan hw_clks = max77620_hw_clks_info;
1975a227cd1SLaxman Dewangan break;
1985a227cd1SLaxman Dewangan
1998ad313feSLaxman Dewangan default:
2008ad313feSLaxman Dewangan dev_err(dev, "Unknown Chip ID\n");
2018ad313feSLaxman Dewangan return -EINVAL;
2028ad313feSLaxman Dewangan }
2038ad313feSLaxman Dewangan
2041c703225SJavier Martinez Canillas drv_data->num_clks = num_clks;
2058ad313feSLaxman Dewangan drv_data->max_clk_data = devm_kcalloc(dev, num_clks,
2068ad313feSLaxman Dewangan sizeof(*drv_data->max_clk_data),
2078ad313feSLaxman Dewangan GFP_KERNEL);
2088ad313feSLaxman Dewangan if (!drv_data->max_clk_data)
2098ad313feSLaxman Dewangan return -ENOMEM;
2108ad313feSLaxman Dewangan
2118ad313feSLaxman Dewangan for (i = 0; i < num_clks; i++) {
2128ad313feSLaxman Dewangan struct max77686_clk_init_data *max_clk_data;
2138ad313feSLaxman Dewangan const char *clk_name;
2148ad313feSLaxman Dewangan
2158ad313feSLaxman Dewangan max_clk_data = &drv_data->max_clk_data[i];
2168ad313feSLaxman Dewangan
2178ad313feSLaxman Dewangan max_clk_data->regmap = regmap;
2188ad313feSLaxman Dewangan max_clk_data->clk_info = &hw_clks[i];
2198ad313feSLaxman Dewangan max_clk_data->clk_idata.flags = hw_clks[i].flags;
2208ad313feSLaxman Dewangan max_clk_data->clk_idata.ops = &max77686_clk_ops;
2218ad313feSLaxman Dewangan
2228ad313feSLaxman Dewangan if (parent->of_node &&
2238ad313feSLaxman Dewangan !of_property_read_string_index(parent->of_node,
2248ad313feSLaxman Dewangan "clock-output-names",
2258ad313feSLaxman Dewangan i, &clk_name))
2268ad313feSLaxman Dewangan max_clk_data->clk_idata.name = clk_name;
2278ad313feSLaxman Dewangan else
2288ad313feSLaxman Dewangan max_clk_data->clk_idata.name = hw_clks[i].name;
2298ad313feSLaxman Dewangan
2308ad313feSLaxman Dewangan max_clk_data->hw.init = &max_clk_data->clk_idata;
2318ad313feSLaxman Dewangan
2329b4cac33SStephen Boyd ret = devm_clk_hw_register(dev, &max_clk_data->hw);
2339b4cac33SStephen Boyd if (ret) {
2348ad313feSLaxman Dewangan dev_err(dev, "Failed to clock register: %d\n", ret);
2358ad313feSLaxman Dewangan return ret;
2368ad313feSLaxman Dewangan }
2378ad313feSLaxman Dewangan
238*8144e1e8SMatti Vaittinen ret = devm_clk_hw_register_clkdev(dev, &max_clk_data->hw,
239*8144e1e8SMatti Vaittinen max_clk_data->clk_idata.name,
240*8144e1e8SMatti Vaittinen NULL);
2418ad313feSLaxman Dewangan if (ret < 0) {
2428ad313feSLaxman Dewangan dev_err(dev, "Failed to clkdev register: %d\n", ret);
2438ad313feSLaxman Dewangan return ret;
2448ad313feSLaxman Dewangan }
2458ad313feSLaxman Dewangan }
2468ad313feSLaxman Dewangan
2478ad313feSLaxman Dewangan if (parent->of_node) {
248*8144e1e8SMatti Vaittinen ret = devm_of_clk_add_hw_provider(dev, of_clk_max77686_get,
2499b4cac33SStephen Boyd drv_data);
2508ad313feSLaxman Dewangan
2518ad313feSLaxman Dewangan if (ret < 0) {
2528ad313feSLaxman Dewangan dev_err(dev, "Failed to register OF clock provider: %d\n",
2538ad313feSLaxman Dewangan ret);
2548ad313feSLaxman Dewangan return ret;
2558ad313feSLaxman Dewangan }
2568ad313feSLaxman Dewangan }
2578ad313feSLaxman Dewangan
2588ad313feSLaxman Dewangan /* MAX77802: Enable low-jitter mode on the 32khz clocks. */
2598ad313feSLaxman Dewangan if (drv_data->chip == CHIP_MAX77802) {
2608ad313feSLaxman Dewangan ret = regmap_update_bits(regmap, MAX77802_REG_32KHZ,
2618ad313feSLaxman Dewangan 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT,
2628ad313feSLaxman Dewangan 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT);
2638ad313feSLaxman Dewangan if (ret < 0) {
2648ad313feSLaxman Dewangan dev_err(dev, "Failed to config low-jitter: %d\n", ret);
2658ad313feSLaxman Dewangan return ret;
26673118e61SJonghwa Lee }
267*8144e1e8SMatti Vaittinen }
2688ad313feSLaxman Dewangan
2698ad313feSLaxman Dewangan return 0;
27073118e61SJonghwa Lee }
27173118e61SJonghwa Lee
27273118e61SJonghwa Lee static const struct platform_device_id max77686_clk_id[] = {
2738ad313feSLaxman Dewangan { "max77686-clk", .driver_data = CHIP_MAX77686, },
2748ad313feSLaxman Dewangan { "max77802-clk", .driver_data = CHIP_MAX77802, },
2755a227cd1SLaxman Dewangan { "max77620-clock", .driver_data = CHIP_MAX77620, },
27673118e61SJonghwa Lee {},
27773118e61SJonghwa Lee };
27873118e61SJonghwa Lee MODULE_DEVICE_TABLE(platform, max77686_clk_id);
27973118e61SJonghwa Lee
28073118e61SJonghwa Lee static struct platform_driver max77686_clk_driver = {
28173118e61SJonghwa Lee .driver = {
28273118e61SJonghwa Lee .name = "max77686-clk",
28373118e61SJonghwa Lee },
28473118e61SJonghwa Lee .probe = max77686_clk_probe,
28573118e61SJonghwa Lee .id_table = max77686_clk_id,
28673118e61SJonghwa Lee };
28773118e61SJonghwa Lee
2881887d693SJavier Martinez Canillas module_platform_driver(max77686_clk_driver);
28973118e61SJonghwa Lee
29073118e61SJonghwa Lee MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
29173118e61SJonghwa Lee MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
29273118e61SJonghwa Lee MODULE_LICENSE("GPL");
293