xref: /linux/drivers/clk/clk-max77686.c (revision 1c7032258d568f9a7aeb4c541786699d9a219a2a)
173118e61SJonghwa Lee /*
28ad313feSLaxman Dewangan  * clk-max77686.c - Clock driver for Maxim 77686/MAX77802
373118e61SJonghwa Lee  *
473118e61SJonghwa Lee  * Copyright (C) 2012 Samsung Electornics
573118e61SJonghwa Lee  * Jonghwa Lee <jonghwa3.lee@samsung.com>
673118e61SJonghwa Lee  *
773118e61SJonghwa Lee  * This program is free software; you can redistribute  it and/or modify it
873118e61SJonghwa Lee  * under  the terms of  the GNU General  Public License as published by the
973118e61SJonghwa Lee  * Free Software Foundation;  either version 2 of the  License, or (at your
1073118e61SJonghwa Lee  * option) any later version.
1173118e61SJonghwa Lee  *
1273118e61SJonghwa Lee  * This program is distributed in the hope that it will be useful,
1373118e61SJonghwa Lee  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1473118e61SJonghwa Lee  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1573118e61SJonghwa Lee  * GNU General Public License for more details.
1673118e61SJonghwa Lee  *
1773118e61SJonghwa Lee  * You should have received a copy of the GNU General Public License
1873118e61SJonghwa Lee  * along with this program; if not, write to the Free Software
1973118e61SJonghwa Lee  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2073118e61SJonghwa Lee  *
2173118e61SJonghwa Lee  */
2273118e61SJonghwa Lee 
2373118e61SJonghwa Lee #include <linux/kernel.h>
2473118e61SJonghwa Lee #include <linux/slab.h>
2573118e61SJonghwa Lee #include <linux/err.h>
26a0c4dfeeSPaul Gortmaker #include <linux/module.h>
2773118e61SJonghwa Lee #include <linux/platform_device.h>
285a227cd1SLaxman Dewangan #include <linux/mfd/max77620.h>
2973118e61SJonghwa Lee #include <linux/mfd/max77686.h>
3073118e61SJonghwa Lee #include <linux/mfd/max77686-private.h>
3173118e61SJonghwa Lee #include <linux/clk-provider.h>
3273118e61SJonghwa Lee #include <linux/mutex.h>
3373118e61SJonghwa Lee #include <linux/clkdev.h>
348ad313feSLaxman Dewangan #include <linux/of.h>
358ad313feSLaxman Dewangan #include <linux/regmap.h>
3673118e61SJonghwa Lee 
37a8a76f56SJavier Martinez Canillas #include <dt-bindings/clock/maxim,max77686.h>
388ad313feSLaxman Dewangan #include <dt-bindings/clock/maxim,max77802.h>
395a227cd1SLaxman Dewangan #include <dt-bindings/clock/maxim,max77620.h>
4073118e61SJonghwa Lee 
418ad313feSLaxman Dewangan #define MAX77802_CLOCK_LOW_JITTER_SHIFT 0x3
428ad313feSLaxman Dewangan 
438ad313feSLaxman Dewangan enum max77686_chip_name {
448ad313feSLaxman Dewangan 	CHIP_MAX77686,
458ad313feSLaxman Dewangan 	CHIP_MAX77802,
465a227cd1SLaxman Dewangan 	CHIP_MAX77620,
478ad313feSLaxman Dewangan };
488ad313feSLaxman Dewangan 
498ad313feSLaxman Dewangan struct max77686_hw_clk_info {
508ad313feSLaxman Dewangan 	const char *name;
518ad313feSLaxman Dewangan 	u32 clk_reg;
528ad313feSLaxman Dewangan 	u32 clk_enable_mask;
538ad313feSLaxman Dewangan 	u32 flags;
548ad313feSLaxman Dewangan };
558ad313feSLaxman Dewangan 
568ad313feSLaxman Dewangan struct max77686_clk_init_data {
578ad313feSLaxman Dewangan 	struct regmap *regmap;
588ad313feSLaxman Dewangan 	struct clk_hw hw;
598ad313feSLaxman Dewangan 	struct clk_init_data clk_idata;
608ad313feSLaxman Dewangan 	const struct max77686_hw_clk_info *clk_info;
618ad313feSLaxman Dewangan };
628ad313feSLaxman Dewangan 
638ad313feSLaxman Dewangan struct max77686_clk_driver_data {
648ad313feSLaxman Dewangan 	enum max77686_chip_name chip;
658ad313feSLaxman Dewangan 	struct max77686_clk_init_data *max_clk_data;
669b4cac33SStephen Boyd 	size_t num_clks;
678ad313feSLaxman Dewangan };
688ad313feSLaxman Dewangan 
698ad313feSLaxman Dewangan static const struct
708ad313feSLaxman Dewangan max77686_hw_clk_info max77686_hw_clks_info[MAX77686_CLKS_NUM] = {
7173118e61SJonghwa Lee 	[MAX77686_CLK_AP] = {
7273118e61SJonghwa Lee 		.name = "32khz_ap",
738ad313feSLaxman Dewangan 		.clk_reg = MAX77686_REG_32KHZ,
748ad313feSLaxman Dewangan 		.clk_enable_mask = BIT(MAX77686_CLK_AP),
7573118e61SJonghwa Lee 	},
7673118e61SJonghwa Lee 	[MAX77686_CLK_CP] = {
7773118e61SJonghwa Lee 		.name = "32khz_cp",
788ad313feSLaxman Dewangan 		.clk_reg = MAX77686_REG_32KHZ,
798ad313feSLaxman Dewangan 		.clk_enable_mask = BIT(MAX77686_CLK_CP),
8073118e61SJonghwa Lee 	},
8173118e61SJonghwa Lee 	[MAX77686_CLK_PMIC] = {
8273118e61SJonghwa Lee 		.name = "32khz_pmic",
838ad313feSLaxman Dewangan 		.clk_reg = MAX77686_REG_32KHZ,
848ad313feSLaxman Dewangan 		.clk_enable_mask = BIT(MAX77686_CLK_PMIC),
8573118e61SJonghwa Lee 	},
8673118e61SJonghwa Lee };
8773118e61SJonghwa Lee 
888ad313feSLaxman Dewangan static const struct
898ad313feSLaxman Dewangan max77686_hw_clk_info max77802_hw_clks_info[MAX77802_CLKS_NUM] = {
908ad313feSLaxman Dewangan 	[MAX77802_CLK_32K_AP] = {
918ad313feSLaxman Dewangan 		.name = "32khz_ap",
928ad313feSLaxman Dewangan 		.clk_reg = MAX77802_REG_32KHZ,
938ad313feSLaxman Dewangan 		.clk_enable_mask = BIT(MAX77802_CLK_32K_AP),
948ad313feSLaxman Dewangan 	},
958ad313feSLaxman Dewangan 	[MAX77802_CLK_32K_CP] = {
968ad313feSLaxman Dewangan 		.name = "32khz_cp",
978ad313feSLaxman Dewangan 		.clk_reg = MAX77802_REG_32KHZ,
988ad313feSLaxman Dewangan 		.clk_enable_mask = BIT(MAX77802_CLK_32K_CP),
998ad313feSLaxman Dewangan 	},
1008ad313feSLaxman Dewangan };
1018ad313feSLaxman Dewangan 
1025a227cd1SLaxman Dewangan static const struct
1035a227cd1SLaxman Dewangan max77686_hw_clk_info max77620_hw_clks_info[MAX77620_CLKS_NUM] = {
1045a227cd1SLaxman Dewangan 	[MAX77620_CLK_32K_OUT0] = {
1055a227cd1SLaxman Dewangan 		.name = "32khz_out0",
1065a227cd1SLaxman Dewangan 		.clk_reg = MAX77620_REG_CNFG1_32K,
1075a227cd1SLaxman Dewangan 		.clk_enable_mask = MAX77620_CNFG1_32K_OUT0_EN,
1085a227cd1SLaxman Dewangan 	},
1095a227cd1SLaxman Dewangan };
1105a227cd1SLaxman Dewangan 
1118ad313feSLaxman Dewangan static struct max77686_clk_init_data *to_max77686_clk_init_data(
1128ad313feSLaxman Dewangan 				struct clk_hw *hw)
1138ad313feSLaxman Dewangan {
1148ad313feSLaxman Dewangan 	return container_of(hw, struct max77686_clk_init_data, hw);
1158ad313feSLaxman Dewangan }
1168ad313feSLaxman Dewangan 
1178ad313feSLaxman Dewangan static int max77686_clk_prepare(struct clk_hw *hw)
1188ad313feSLaxman Dewangan {
1198ad313feSLaxman Dewangan 	struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
1208ad313feSLaxman Dewangan 
1218ad313feSLaxman Dewangan 	return regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
1228ad313feSLaxman Dewangan 				  max77686->clk_info->clk_enable_mask,
1238ad313feSLaxman Dewangan 				  max77686->clk_info->clk_enable_mask);
1248ad313feSLaxman Dewangan }
1258ad313feSLaxman Dewangan 
1268ad313feSLaxman Dewangan static void max77686_clk_unprepare(struct clk_hw *hw)
1278ad313feSLaxman Dewangan {
1288ad313feSLaxman Dewangan 	struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
1298ad313feSLaxman Dewangan 
1308ad313feSLaxman Dewangan 	regmap_update_bits(max77686->regmap, max77686->clk_info->clk_reg,
1318ad313feSLaxman Dewangan 			   max77686->clk_info->clk_enable_mask,
1328ad313feSLaxman Dewangan 			   ~max77686->clk_info->clk_enable_mask);
1338ad313feSLaxman Dewangan }
1348ad313feSLaxman Dewangan 
1358ad313feSLaxman Dewangan static int max77686_clk_is_prepared(struct clk_hw *hw)
1368ad313feSLaxman Dewangan {
1378ad313feSLaxman Dewangan 	struct max77686_clk_init_data *max77686 = to_max77686_clk_init_data(hw);
1388ad313feSLaxman Dewangan 	int ret;
1398ad313feSLaxman Dewangan 	u32 val;
1408ad313feSLaxman Dewangan 
1418ad313feSLaxman Dewangan 	ret = regmap_read(max77686->regmap, max77686->clk_info->clk_reg, &val);
1428ad313feSLaxman Dewangan 
1438ad313feSLaxman Dewangan 	if (ret < 0)
1448ad313feSLaxman Dewangan 		return -EINVAL;
1458ad313feSLaxman Dewangan 
1468ad313feSLaxman Dewangan 	return val & max77686->clk_info->clk_enable_mask;
1478ad313feSLaxman Dewangan }
1488ad313feSLaxman Dewangan 
1498ad313feSLaxman Dewangan static unsigned long max77686_recalc_rate(struct clk_hw *hw,
1508ad313feSLaxman Dewangan 					  unsigned long parent_rate)
1518ad313feSLaxman Dewangan {
1528ad313feSLaxman Dewangan 	return 32768;
1538ad313feSLaxman Dewangan }
1548ad313feSLaxman Dewangan 
1558ad313feSLaxman Dewangan static struct clk_ops max77686_clk_ops = {
1568ad313feSLaxman Dewangan 	.prepare	= max77686_clk_prepare,
1578ad313feSLaxman Dewangan 	.unprepare	= max77686_clk_unprepare,
1588ad313feSLaxman Dewangan 	.is_prepared	= max77686_clk_is_prepared,
1598ad313feSLaxman Dewangan 	.recalc_rate	= max77686_recalc_rate,
1608ad313feSLaxman Dewangan };
1618ad313feSLaxman Dewangan 
1629b4cac33SStephen Boyd static struct clk_hw *
1639b4cac33SStephen Boyd of_clk_max77686_get(struct of_phandle_args *clkspec, void *data)
1649b4cac33SStephen Boyd {
1659b4cac33SStephen Boyd 	struct max77686_clk_driver_data *drv_data = data;
1669b4cac33SStephen Boyd 	unsigned int idx = clkspec->args[0];
1679b4cac33SStephen Boyd 
1689b4cac33SStephen Boyd 	if (idx >= drv_data->num_clks) {
1699b4cac33SStephen Boyd 		pr_err("%s: invalid index %u\n", __func__, idx);
1709b4cac33SStephen Boyd 		return ERR_PTR(-EINVAL);
1719b4cac33SStephen Boyd 	}
1729b4cac33SStephen Boyd 
1739b4cac33SStephen Boyd 	return &drv_data->max_clk_data[idx].hw;
1749b4cac33SStephen Boyd }
1759b4cac33SStephen Boyd 
176018ae93fSBill Pemberton static int max77686_clk_probe(struct platform_device *pdev)
17773118e61SJonghwa Lee {
1788ad313feSLaxman Dewangan 	struct device *dev = &pdev->dev;
1798ad313feSLaxman Dewangan 	struct device *parent = dev->parent;
1808ad313feSLaxman Dewangan 	const struct platform_device_id *id = platform_get_device_id(pdev);
1818ad313feSLaxman Dewangan 	struct max77686_clk_driver_data *drv_data;
1828ad313feSLaxman Dewangan 	const struct max77686_hw_clk_info *hw_clks;
1838ad313feSLaxman Dewangan 	struct regmap *regmap;
1848ad313feSLaxman Dewangan 	int i, ret, num_clks;
18573118e61SJonghwa Lee 
1868ad313feSLaxman Dewangan 	drv_data = devm_kzalloc(dev, sizeof(*drv_data), GFP_KERNEL);
1878ad313feSLaxman Dewangan 	if (!drv_data)
1888ad313feSLaxman Dewangan 		return -ENOMEM;
1898ad313feSLaxman Dewangan 
1908ad313feSLaxman Dewangan 	regmap = dev_get_regmap(parent, NULL);
1918ad313feSLaxman Dewangan 	if (!regmap) {
1928ad313feSLaxman Dewangan 		dev_err(dev, "Failed to get rtc regmap\n");
1938ad313feSLaxman Dewangan 		return -ENODEV;
1948ad313feSLaxman Dewangan 	}
1958ad313feSLaxman Dewangan 
1968ad313feSLaxman Dewangan 	drv_data->chip = id->driver_data;
1978ad313feSLaxman Dewangan 
1988ad313feSLaxman Dewangan 	switch (drv_data->chip) {
1998ad313feSLaxman Dewangan 	case CHIP_MAX77686:
2008ad313feSLaxman Dewangan 		num_clks = MAX77686_CLKS_NUM;
2018ad313feSLaxman Dewangan 		hw_clks = max77686_hw_clks_info;
2028ad313feSLaxman Dewangan 		break;
2038ad313feSLaxman Dewangan 
2048ad313feSLaxman Dewangan 	case CHIP_MAX77802:
2058ad313feSLaxman Dewangan 		num_clks = MAX77802_CLKS_NUM;
2068ad313feSLaxman Dewangan 		hw_clks = max77802_hw_clks_info;
2078ad313feSLaxman Dewangan 		break;
2088ad313feSLaxman Dewangan 
2095a227cd1SLaxman Dewangan 	case CHIP_MAX77620:
2105a227cd1SLaxman Dewangan 		num_clks = MAX77620_CLKS_NUM;
2115a227cd1SLaxman Dewangan 		hw_clks = max77620_hw_clks_info;
2125a227cd1SLaxman Dewangan 		break;
2135a227cd1SLaxman Dewangan 
2148ad313feSLaxman Dewangan 	default:
2158ad313feSLaxman Dewangan 		dev_err(dev, "Unknown Chip ID\n");
2168ad313feSLaxman Dewangan 		return -EINVAL;
2178ad313feSLaxman Dewangan 	}
2188ad313feSLaxman Dewangan 
219*1c703225SJavier Martinez Canillas 	drv_data->num_clks = num_clks;
2208ad313feSLaxman Dewangan 	drv_data->max_clk_data = devm_kcalloc(dev, num_clks,
2218ad313feSLaxman Dewangan 					      sizeof(*drv_data->max_clk_data),
2228ad313feSLaxman Dewangan 					      GFP_KERNEL);
2238ad313feSLaxman Dewangan 	if (!drv_data->max_clk_data)
2248ad313feSLaxman Dewangan 		return -ENOMEM;
2258ad313feSLaxman Dewangan 
2268ad313feSLaxman Dewangan 	for (i = 0; i < num_clks; i++) {
2278ad313feSLaxman Dewangan 		struct max77686_clk_init_data *max_clk_data;
2288ad313feSLaxman Dewangan 		const char *clk_name;
2298ad313feSLaxman Dewangan 
2308ad313feSLaxman Dewangan 		max_clk_data = &drv_data->max_clk_data[i];
2318ad313feSLaxman Dewangan 
2328ad313feSLaxman Dewangan 		max_clk_data->regmap = regmap;
2338ad313feSLaxman Dewangan 		max_clk_data->clk_info = &hw_clks[i];
2348ad313feSLaxman Dewangan 		max_clk_data->clk_idata.flags = hw_clks[i].flags;
2358ad313feSLaxman Dewangan 		max_clk_data->clk_idata.ops = &max77686_clk_ops;
2368ad313feSLaxman Dewangan 
2378ad313feSLaxman Dewangan 		if (parent->of_node &&
2388ad313feSLaxman Dewangan 		    !of_property_read_string_index(parent->of_node,
2398ad313feSLaxman Dewangan 						   "clock-output-names",
2408ad313feSLaxman Dewangan 						   i, &clk_name))
2418ad313feSLaxman Dewangan 			max_clk_data->clk_idata.name = clk_name;
2428ad313feSLaxman Dewangan 		else
2438ad313feSLaxman Dewangan 			max_clk_data->clk_idata.name = hw_clks[i].name;
2448ad313feSLaxman Dewangan 
2458ad313feSLaxman Dewangan 		max_clk_data->hw.init = &max_clk_data->clk_idata;
2468ad313feSLaxman Dewangan 
2479b4cac33SStephen Boyd 		ret = devm_clk_hw_register(dev, &max_clk_data->hw);
2489b4cac33SStephen Boyd 		if (ret) {
2498ad313feSLaxman Dewangan 			dev_err(dev, "Failed to clock register: %d\n", ret);
2508ad313feSLaxman Dewangan 			return ret;
2518ad313feSLaxman Dewangan 		}
2528ad313feSLaxman Dewangan 
2539b4cac33SStephen Boyd 		ret = clk_hw_register_clkdev(&max_clk_data->hw,
2549b4cac33SStephen Boyd 					     max_clk_data->clk_idata.name, NULL);
2558ad313feSLaxman Dewangan 		if (ret < 0) {
2568ad313feSLaxman Dewangan 			dev_err(dev, "Failed to clkdev register: %d\n", ret);
2578ad313feSLaxman Dewangan 			return ret;
2588ad313feSLaxman Dewangan 		}
2598ad313feSLaxman Dewangan 	}
2608ad313feSLaxman Dewangan 
2618ad313feSLaxman Dewangan 	if (parent->of_node) {
2629b4cac33SStephen Boyd 		ret = of_clk_add_hw_provider(parent->of_node, of_clk_max77686_get,
2639b4cac33SStephen Boyd 					     drv_data);
2648ad313feSLaxman Dewangan 
2658ad313feSLaxman Dewangan 		if (ret < 0) {
2668ad313feSLaxman Dewangan 			dev_err(dev, "Failed to register OF clock provider: %d\n",
2678ad313feSLaxman Dewangan 				ret);
2688ad313feSLaxman Dewangan 			return ret;
2698ad313feSLaxman Dewangan 		}
2708ad313feSLaxman Dewangan 	}
2718ad313feSLaxman Dewangan 
2728ad313feSLaxman Dewangan 	/* MAX77802: Enable low-jitter mode on the 32khz clocks. */
2738ad313feSLaxman Dewangan 	if (drv_data->chip == CHIP_MAX77802) {
2748ad313feSLaxman Dewangan 		ret = regmap_update_bits(regmap, MAX77802_REG_32KHZ,
2758ad313feSLaxman Dewangan 					 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT,
2768ad313feSLaxman Dewangan 					 1 << MAX77802_CLOCK_LOW_JITTER_SHIFT);
2778ad313feSLaxman Dewangan 		if (ret < 0) {
2788ad313feSLaxman Dewangan 			dev_err(dev, "Failed to config low-jitter: %d\n", ret);
2798ad313feSLaxman Dewangan 			goto remove_of_clk_provider;
2808ad313feSLaxman Dewangan 		}
2818ad313feSLaxman Dewangan 	}
2828ad313feSLaxman Dewangan 
2838ad313feSLaxman Dewangan 	return 0;
2848ad313feSLaxman Dewangan 
2858ad313feSLaxman Dewangan remove_of_clk_provider:
2868ad313feSLaxman Dewangan 	if (parent->of_node)
2878ad313feSLaxman Dewangan 		of_clk_del_provider(parent->of_node);
2888ad313feSLaxman Dewangan 
2898ad313feSLaxman Dewangan 	return ret;
29073118e61SJonghwa Lee }
29173118e61SJonghwa Lee 
2921fc7ad5dSBill Pemberton static int max77686_clk_remove(struct platform_device *pdev)
29373118e61SJonghwa Lee {
2948ad313feSLaxman Dewangan 	struct device *parent = pdev->dev.parent;
2958ad313feSLaxman Dewangan 
2968ad313feSLaxman Dewangan 	if (parent->of_node)
2978ad313feSLaxman Dewangan 		of_clk_del_provider(parent->of_node);
2988ad313feSLaxman Dewangan 
2998ad313feSLaxman Dewangan 	return 0;
30073118e61SJonghwa Lee }
30173118e61SJonghwa Lee 
30273118e61SJonghwa Lee static const struct platform_device_id max77686_clk_id[] = {
3038ad313feSLaxman Dewangan 	{ "max77686-clk", .driver_data = CHIP_MAX77686, },
3048ad313feSLaxman Dewangan 	{ "max77802-clk", .driver_data = CHIP_MAX77802, },
3055a227cd1SLaxman Dewangan 	{ "max77620-clock", .driver_data = CHIP_MAX77620, },
30673118e61SJonghwa Lee 	{},
30773118e61SJonghwa Lee };
30873118e61SJonghwa Lee MODULE_DEVICE_TABLE(platform, max77686_clk_id);
30973118e61SJonghwa Lee 
31073118e61SJonghwa Lee static struct platform_driver max77686_clk_driver = {
31173118e61SJonghwa Lee 	.driver = {
31273118e61SJonghwa Lee 		.name  = "max77686-clk",
31373118e61SJonghwa Lee 	},
31473118e61SJonghwa Lee 	.probe = max77686_clk_probe,
315f9cfa630SBill Pemberton 	.remove = max77686_clk_remove,
31673118e61SJonghwa Lee 	.id_table = max77686_clk_id,
31773118e61SJonghwa Lee };
31873118e61SJonghwa Lee 
3191887d693SJavier Martinez Canillas module_platform_driver(max77686_clk_driver);
32073118e61SJonghwa Lee 
32173118e61SJonghwa Lee MODULE_DESCRIPTION("MAXIM 77686 Clock Driver");
32273118e61SJonghwa Lee MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
32373118e61SJonghwa Lee MODULE_LICENSE("GPL");
324