18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2015 Endless Mobile, Inc.
48c2ecf20Sopenharmony_ci * Author: Carlo Caione <carlo@endlessm.com>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef __MESON_PARM_H
88c2ecf20Sopenharmony_ci#define __MESON_PARM_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/bits.h>
118c2ecf20Sopenharmony_ci#include <linux/regmap.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define PMASK(width)			GENMASK(width - 1, 0)
148c2ecf20Sopenharmony_ci#define SETPMASK(width, shift)		GENMASK(shift + width - 1, shift)
158c2ecf20Sopenharmony_ci#define CLRPMASK(width, shift)		(~SETPMASK(width, shift))
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define PARM_GET(width, shift, reg)					\
188c2ecf20Sopenharmony_ci	(((reg) & SETPMASK(width, shift)) >> (shift))
198c2ecf20Sopenharmony_ci#define PARM_SET(width, shift, reg, val)				\
208c2ecf20Sopenharmony_ci	(((reg) & CLRPMASK(width, shift)) | ((val) << (shift)))
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define MESON_PARM_APPLICABLE(p)		(!!((p)->width))
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistruct parm {
258c2ecf20Sopenharmony_ci	u16	reg_off;
268c2ecf20Sopenharmony_ci	u8	shift;
278c2ecf20Sopenharmony_ci	u8	width;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic inline unsigned int meson_parm_read(struct regmap *map, struct parm *p)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	unsigned int val;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	regmap_read(map, p->reg_off, &val);
358c2ecf20Sopenharmony_ci	return PARM_GET(p->width, p->shift, val);
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic inline void meson_parm_write(struct regmap *map, struct parm *p,
398c2ecf20Sopenharmony_ci				    unsigned int val)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	regmap_update_bits(map, p->reg_off, SETPMASK(p->width, p->shift),
428c2ecf20Sopenharmony_ci			   val << p->shift);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#endif /* __MESON_PARM_H */
468c2ecf20Sopenharmony_ci
47