Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Retu/Tahvo MFD driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2004, 2005 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Based on code written by Juha Yrjölä, David Weinehall and Mikko Ylinen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Rewritten by Aaro Koskinen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This file is subject to the terms and conditions of the GNU General
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Public License. See the file "COPYING" in the main directory of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * archive for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/mfd/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/mfd/retu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /* Registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define RETU_REG_ASICR		0x00		/* ASIC ID and revision */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define RETU_REG_ASICR_VILMA	(1 << 7)	/* Bit indicating Vilma */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define RETU_REG_IDR		0x01		/* Interrupt ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define RETU_REG_IMR		0x02		/* Interrupt mask (Retu) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define TAHVO_REG_IMR		0x03		/* Interrupt mask (Tahvo) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /* Interrupt sources */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define RETU_INT_PWR		0		/* Power button */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct retu_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct regmap			*regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct device			*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct mutex			mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct regmap_irq_chip_data	*irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static struct resource retu_pwrbutton_res[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		.name	= "retu-pwrbutton",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		.start	= RETU_INT_PWR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		.end	= RETU_INT_PWR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		.flags	= IORESOURCE_IRQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static const struct mfd_cell retu_devs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		.name		= "retu-wdt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		.name		= "retu-pwrbutton",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		.resources	= retu_pwrbutton_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		.num_resources	= ARRAY_SIZE(retu_pwrbutton_res),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static struct regmap_irq retu_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	[RETU_INT_PWR] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		.mask = 1 << RETU_INT_PWR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static struct regmap_irq_chip retu_irq_chip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.name		= "RETU",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	.irqs		= retu_irqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	.num_irqs	= ARRAY_SIZE(retu_irqs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	.num_regs	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	.status_base	= RETU_REG_IDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	.mask_base	= RETU_REG_IMR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	.ack_base	= RETU_REG_IDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /* Retu device registered for the power off. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static struct retu_dev *retu_pm_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) static struct resource tahvo_usb_res[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		.name	= "tahvo-usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		.start	= TAHVO_INT_VBUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		.end	= TAHVO_INT_VBUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		.flags	= IORESOURCE_IRQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static const struct mfd_cell tahvo_devs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		.name		= "tahvo-usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		.resources	= tahvo_usb_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		.num_resources	= ARRAY_SIZE(tahvo_usb_res),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static struct regmap_irq tahvo_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	[TAHVO_INT_VBUS] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		.mask = 1 << TAHVO_INT_VBUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static struct regmap_irq_chip tahvo_irq_chip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	.name		= "TAHVO",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	.irqs		= tahvo_irqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	.num_irqs	= ARRAY_SIZE(tahvo_irqs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	.num_regs	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	.status_base	= RETU_REG_IDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	.mask_base	= TAHVO_REG_IMR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	.ack_base	= RETU_REG_IDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static const struct retu_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	char			*chip_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	char			*companion_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	struct regmap_irq_chip	*irq_chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	const struct mfd_cell	*children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	int			nchildren;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) } retu_data[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	[0] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		.chip_name	= "Retu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		.companion_name	= "Vilma",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		.irq_chip	= &retu_irq_chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		.children	= retu_devs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.nchildren	= ARRAY_SIZE(retu_devs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	[1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		.chip_name	= "Tahvo",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		.companion_name	= "Betty",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		.irq_chip	= &tahvo_irq_chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		.children	= tahvo_devs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		.nchildren	= ARRAY_SIZE(tahvo_devs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int retu_read(struct retu_dev *rdev, u8 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	mutex_lock(&rdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	ret = regmap_read(rdev->regmap, reg, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	mutex_unlock(&rdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return ret ? ret : value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) EXPORT_SYMBOL_GPL(retu_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	mutex_lock(&rdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = regmap_write(rdev->regmap, reg, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mutex_unlock(&rdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) EXPORT_SYMBOL_GPL(retu_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void retu_power_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	struct retu_dev *rdev = retu_pm_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	mutex_lock(&retu_pm_power_off->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	/* Ignore power button state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	regmap_read(rdev->regmap, RETU_REG_CC1, &reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	regmap_write(rdev->regmap, RETU_REG_CC1, reg | 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	/* Expire watchdog immediately */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	regmap_write(rdev->regmap, RETU_REG_WATCHDOG, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	/* Wait for poweroff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	for (;;)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	mutex_unlock(&retu_pm_power_off->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static int retu_regmap_read(void *context, const void *reg, size_t reg_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			    void *val, size_t val_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	struct device *dev = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct i2c_client *i2c = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	BUG_ON(reg_size != 1 || val_size != 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	ret = i2c_smbus_read_word_data(i2c, *(u8 const *)reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	*(u16 *)val = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int retu_regmap_write(void *context, const void *data, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	u8 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	u16 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct device *dev = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct i2c_client *i2c = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	BUG_ON(count != sizeof(reg) + sizeof(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	memcpy(&reg, data, sizeof(reg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	memcpy(&val, data + sizeof(reg), sizeof(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return i2c_smbus_write_word_data(i2c, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static struct regmap_bus retu_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	.read = retu_regmap_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	.write = retu_regmap_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	.val_format_endian_default = REGMAP_ENDIAN_NATIVE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static const struct regmap_config retu_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	.val_bits = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int retu_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct retu_data const *rdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct retu_dev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (i2c->addr > ARRAY_SIZE(retu_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	rdat = &retu_data[i2c->addr - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	rdev = devm_kzalloc(&i2c->dev, sizeof(*rdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (rdev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	i2c_set_clientdata(i2c, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	rdev->dev = &i2c->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	mutex_init(&rdev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	rdev->regmap = devm_regmap_init(&i2c->dev, &retu_bus, &i2c->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 					&retu_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (IS_ERR(rdev->regmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		return PTR_ERR(rdev->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	ret = retu_read(rdev, RETU_REG_ASICR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		dev_err(rdev->dev, "could not read %s revision: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			rdat->chip_name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	dev_info(rdev->dev, "%s%s%s v%d.%d found\n", rdat->chip_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		 (ret & RETU_REG_ASICR_VILMA) ? " & " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 (ret & RETU_REG_ASICR_VILMA) ? rdat->companion_name : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 (ret >> 4) & 0x7, ret & 0xf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	/* Mask all interrupts. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	ret = retu_write(rdev, rdat->irq_chip->mask_base, 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	ret = regmap_add_irq_chip(rdev->regmap, i2c->irq, IRQF_ONESHOT, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				  rdat->irq_chip, &rdev->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	ret = mfd_add_devices(rdev->dev, -1, rdat->children, rdat->nchildren,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			      NULL, regmap_irq_chip_get_base(rdev->irq_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		regmap_del_irq_chip(i2c->irq, rdev->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (i2c->addr == 1 && !pm_power_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		retu_pm_power_off = rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		pm_power_off	  = retu_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int retu_remove(struct i2c_client *i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	struct retu_dev *rdev = i2c_get_clientdata(i2c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (retu_pm_power_off == rdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		pm_power_off	  = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		retu_pm_power_off = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	mfd_remove_devices(rdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	regmap_del_irq_chip(i2c->irq, rdev->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static const struct i2c_device_id retu_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	{ "retu", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	{ "tahvo", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) MODULE_DEVICE_TABLE(i2c, retu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static const struct of_device_id retu_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	{ .compatible = "nokia,retu" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	{ .compatible = "nokia,tahvo" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) MODULE_DEVICE_TABLE(of, retu_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static struct i2c_driver retu_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		.name = "retu-mfd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		.of_match_table = retu_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	.probe		= retu_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	.remove		= retu_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	.id_table	= retu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) module_i2c_driver(retu_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) MODULE_DESCRIPTION("Retu MFD driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) MODULE_AUTHOR("Juha Yrjölä");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) MODULE_AUTHOR("David Weinehall");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) MODULE_AUTHOR("Mikko Ylinen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) MODULE_LICENSE("GPL");