| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Maxim MAX7360 Core Driver |
| 4 |
* |
| 5 |
* Copyright 2025 Bootlin |
| 6 |
* |
| 7 |
* Authors: |
| 8 |
* Kamel Bouhara <[email protected]> |
| 9 |
* Mathieu Dubois-Briand <[email protected]> |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/array_size.h> |
| 13 |
#include <linux/bits.h> |
| 14 |
#include <linux/delay.h> |
| 15 |
#include <linux/device/devres.h> |
| 16 |
#include <linux/dev_printk.h> |
| 17 |
#include <linux/err.h> |
| 18 |
#include <linux/i2c.h> |
| 19 |
#include <linux/interrupt.h> |
| 20 |
#include <linux/mfd/core.h> |
| 21 |
#include <linux/mfd/max7360.h> |
| 22 |
#include <linux/mod_devicetable.h> |
| 23 |
#include <linux/module.h> |
| 24 |
#include <linux/regmap.h> |
| 25 |
#include <linux/types.h> |
| 26 |
|
| 27 |
static const struct mfd_cell max7360_cells[] = { |
| 28 |
{ .name = "max7360-pinctrl" }, |
| 29 |
{ .name = "max7360-pwm" }, |
| 30 |
{ .name = "max7360-keypad" }, |
| 31 |
{ .name = "max7360-rotary" }, |
| 32 |
{ |
| 33 |
.name = "max7360-gpo", |
| 34 |
.of_compatible = "maxim,max7360-gpo", |
| 35 |
}, |
| 36 |
{ |
| 37 |
.name = "max7360-gpio", |
| 38 |
.of_compatible = "maxim,max7360-gpio", |
| 39 |
}, |
| 40 |
}; |
| 41 |
|
| 42 |
static const struct regmap_range max7360_volatile_ranges[] = { |
| 43 |
regmap_reg_range(MAX7360_REG_KEYFIFO, MAX7360_REG_KEYFIFO), |
| 44 |
regmap_reg_range(MAX7360_REG_I2C_TIMEOUT, MAX7360_REG_RTR_CNT), |
| 45 |
}; |
| 46 |
|
| 47 |
static const struct regmap_access_table max7360_volatile_table = { |
| 48 |
.yes_ranges = max7360_volatile_ranges, |
| 49 |
.n_yes_ranges = ARRAY_SIZE(max7360_volatile_ranges), |
| 50 |
}; |
| 51 |
|
| 52 |
static const struct regmap_config max7360_regmap_config = { |
| 53 |
.reg_bits = 8, |
| 54 |
.val_bits = 8, |
| 55 |
.max_register = MAX7360_REG_PWMCFG(MAX7360_PORT_PWM_COUNT - 1), |
| 56 |
.volatile_table = &max7360_volatile_table, |
| 57 |
.cache_type = REGCACHE_MAPLE, |
| 58 |
}; |
| 59 |
|
| 60 |
static int max7360_mask_irqs(struct regmap *regmap) |
| 61 |
{ |
| 62 |
struct device *dev = regmap_get_device(regmap); |
| 63 |
unsigned int val; |
| 64 |
int ret; |
| 65 |
|
| 66 |
/* |
| 67 |
* GPIO/PWM interrupts are not masked on reset: as the MAX7360 "INTI" |
| 68 |
* interrupt line is shared between GPIOs and rotary encoder, this could |
| 69 |
* result in repeated spurious interrupts on the rotary encoder driver |
| 70 |
* if the GPIO driver is not loaded. Mask them now to avoid this |
| 71 |
* situation. |
| 72 |
*/ |
| 73 |
for (unsigned int i = 0; i < MAX7360_PORT_PWM_COUNT; i++) { |
| 74 |
ret = regmap_write_bits(regmap, MAX7360_REG_PWMCFG(i), |
| 75 |
MAX7360_PORT_CFG_INTERRUPT_MASK, |
| 76 |
MAX7360_PORT_CFG_INTERRUPT_MASK); |
| 77 |
if (ret) |
| 78 |
return dev_err_probe(dev, ret, |
| 79 |
"Failed to write MAX7360 port configuration\n"); |
| 80 |
} |
| 81 |
|
| 82 |
/* Read GPIO in register, to ACK any pending IRQ. */ |
| 83 |
ret = regmap_read(regmap, MAX7360_REG_GPIOIN, &val); |
| 84 |
if (ret) |
| 85 |
return dev_err_probe(dev, ret, "Failed to read GPIO values\n"); |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
static int max7360_reset(struct regmap *regmap) |
| 91 |
{ |
| 92 |
struct device *dev = regmap_get_device(regmap); |
| 93 |
int ret; |
| 94 |
|
| 95 |
ret = regmap_write(regmap, MAX7360_REG_GPIOCFG, MAX7360_GPIO_CFG_GPIO_RST); |
| 96 |
if (ret) { |
| 97 |
dev_err(dev, "Failed to reset GPIO configuration: %x\n", ret); |
| 98 |
return ret; |
| 99 |
} |
| 100 |
|
| 101 |
ret = regcache_drop_region(regmap, MAX7360_REG_GPIOCFG, MAX7360_REG_GPIO_LAST); |
| 102 |
if (ret) { |
| 103 |
dev_err(dev, "Failed to drop regmap cache: %x\n", ret); |
| 104 |
return ret; |
| 105 |
} |
| 106 |
|
| 107 |
ret = regmap_write(regmap, MAX7360_REG_SLEEP, 0); |
| 108 |
if (ret) { |
| 109 |
dev_err(dev, "Failed to reset autosleep configuration: %x\n", ret); |
| 110 |
return ret; |
| 111 |
} |
| 112 |
|
| 113 |
ret = regmap_write(regmap, MAX7360_REG_DEBOUNCE, 0); |
| 114 |
if (ret) |
| 115 |
dev_err(dev, "Failed to reset GPO port count: %x\n", ret); |
| 116 |
|
| 117 |
return ret; |
| 118 |
} |
| 119 |
|
| 120 |
static int max7360_probe(struct i2c_client *client) |
| 121 |
{ |
| 122 |
struct device *dev = &client->dev; |
| 123 |
struct regmap *regmap; |
| 124 |
int ret; |
| 125 |
|
| 126 |
regmap = devm_regmap_init_i2c(client, &max7360_regmap_config); |
| 127 |
if (IS_ERR(regmap)) |
| 128 |
return dev_err_probe(dev, PTR_ERR(regmap), "Failed to initialise regmap\n"); |
| 129 |
|
| 130 |
ret = max7360_reset(regmap); |
| 131 |
if (ret) |
| 132 |
return dev_err_probe(dev, ret, "Failed to reset device\n"); |
| 133 |
|
| 134 |
/* Get the device out of shutdown mode. */ |
| 135 |
ret = regmap_write_bits(regmap, MAX7360_REG_GPIOCFG, |
| 136 |
MAX7360_GPIO_CFG_GPIO_EN, |
| 137 |
MAX7360_GPIO_CFG_GPIO_EN); |
| 138 |
if (ret) |
| 139 |
return dev_err_probe(dev, ret, "Failed to enable GPIO and PWM module\n"); |
| 140 |
|
| 141 |
ret = max7360_mask_irqs(regmap); |
| 142 |
if (ret) |
| 143 |
return dev_err_probe(dev, ret, "Could not mask interrupts\n"); |
| 144 |
|
| 145 |
ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE, |
| 146 |
max7360_cells, ARRAY_SIZE(max7360_cells), |
| 147 |
NULL, 0, NULL); |
| 148 |
if (ret) |
| 149 |
return dev_err_probe(dev, ret, "Failed to register child devices\n"); |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
static const struct of_device_id max7360_dt_match[] = { |
| 155 |
{ .compatible = "maxim,max7360" }, |
| 156 |
{} |
| 157 |
}; |
| 158 |
MODULE_DEVICE_TABLE(of, max7360_dt_match); |
| 159 |
|
| 160 |
static struct i2c_driver max7360_driver = { |
| 161 |
.driver = { |
| 162 |
.name = "max7360", |
| 163 |
.of_match_table = max7360_dt_match, |
| 164 |
}, |
| 165 |
.probe = max7360_probe, |
| 166 |
}; |
| 167 |
module_i2c_driver(max7360_driver); |
| 168 |
|
| 169 |
MODULE_DESCRIPTION("Maxim MAX7360 I2C IO Expander core driver"); |
| 170 |
MODULE_AUTHOR("Kamel Bouhara <[email protected]>"); |
| 171 |
MODULE_LICENSE("GPL"); |