18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (C) 2018 Microchip Technology Inc,
48c2ecf20Sopenharmony_ci *                     Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
108c2ecf20Sopenharmony_ci#include <linux/of.h>
118c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
128c2ecf20Sopenharmony_ci#include <linux/regmap.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <soc/at91/atmel-sfr.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "pmc.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct clk_i2s_mux {
208c2ecf20Sopenharmony_ci	struct clk_hw hw;
218c2ecf20Sopenharmony_ci	struct regmap *regmap;
228c2ecf20Sopenharmony_ci	u8 bus_id;
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define to_clk_i2s_mux(hw) container_of(hw, struct clk_i2s_mux, hw)
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic u8 clk_i2s_mux_get_parent(struct clk_hw *hw)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	struct clk_i2s_mux *mux = to_clk_i2s_mux(hw);
308c2ecf20Sopenharmony_ci	u32 val;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	regmap_read(mux->regmap, AT91_SFR_I2SCLKSEL, &val);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	return (val & BIT(mux->bus_id)) >> mux->bus_id;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int clk_i2s_mux_set_parent(struct clk_hw *hw, u8 index)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct clk_i2s_mux *mux = to_clk_i2s_mux(hw);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	return regmap_update_bits(mux->regmap, AT91_SFR_I2SCLKSEL,
428c2ecf20Sopenharmony_ci				  BIT(mux->bus_id), index << mux->bus_id);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic const struct clk_ops clk_i2s_mux_ops = {
468c2ecf20Sopenharmony_ci	.get_parent = clk_i2s_mux_get_parent,
478c2ecf20Sopenharmony_ci	.set_parent = clk_i2s_mux_set_parent,
488c2ecf20Sopenharmony_ci	.determine_rate = __clk_mux_determine_rate,
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct clk_hw * __init
528c2ecf20Sopenharmony_ciat91_clk_i2s_mux_register(struct regmap *regmap, const char *name,
538c2ecf20Sopenharmony_ci			  const char * const *parent_names,
548c2ecf20Sopenharmony_ci			  unsigned int num_parents, u8 bus_id)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct clk_init_data init = {};
578c2ecf20Sopenharmony_ci	struct clk_i2s_mux *i2s_ck;
588c2ecf20Sopenharmony_ci	int ret;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	i2s_ck = kzalloc(sizeof(*i2s_ck), GFP_KERNEL);
618c2ecf20Sopenharmony_ci	if (!i2s_ck)
628c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	init.name = name;
658c2ecf20Sopenharmony_ci	init.ops = &clk_i2s_mux_ops;
668c2ecf20Sopenharmony_ci	init.parent_names = parent_names;
678c2ecf20Sopenharmony_ci	init.num_parents = num_parents;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	i2s_ck->hw.init = &init;
708c2ecf20Sopenharmony_ci	i2s_ck->bus_id = bus_id;
718c2ecf20Sopenharmony_ci	i2s_ck->regmap = regmap;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	ret = clk_hw_register(NULL, &i2s_ck->hw);
748c2ecf20Sopenharmony_ci	if (ret) {
758c2ecf20Sopenharmony_ci		kfree(i2s_ck);
768c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return &i2s_ck->hw;
808c2ecf20Sopenharmony_ci}
81