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) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * max1586.c  --  Voltage and current regulation for the Maxim 1586
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2008 Robert Jarzmik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/regulator/max1586.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/regulator/of_regulator.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define MAX1586_V3_MAX_VSEL 31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define MAX1586_V6_MAX_VSEL 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define MAX1586_V3_MIN_UV   700000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define MAX1586_V3_MAX_UV  1475000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define MAX1586_V6_MIN_UV        0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define MAX1586_V6_MAX_UV  3000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define I2C_V3_SELECT (0 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define I2C_V6_SELECT (1 << 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) struct max1586_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	/* min/max V3 voltage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned int min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	unsigned int max_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	unsigned int v3_curr_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	unsigned int v6_curr_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * V6 voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * On I2C bus, sending a "x" byte to the max1586 means :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *   set V6 to either 0V, 1.8V, 2.5V, 3V depending on (x & 0x3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * As regulator framework doesn't accept voltages to be 0V, we use 1uV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static const unsigned int v6_voltages_uv[] = { 1, 1800000, 2500000, 3000000 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * V3 voltage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * On I2C bus, sending a "x" byte to the max1586 means :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *   set V3 to 0.700V + (x & 0x1f) * 0.025V
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * This voltage can be increased by external resistors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * R24 and R25=100kOhm as described in the data sheet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * The gain is approximately: 1 + R24/R25 + R24/185.5kOhm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int max1586_v3_get_voltage_sel(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	return max1586->v3_curr_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static int max1586_v3_set_voltage_sel(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				      unsigned selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct i2c_client *client = max1586->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	u8 v3_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	dev_dbg(&client->dev, "changing voltage v3 to %dmv\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		regulator_list_voltage_linear(rdev, selector) / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	v3_prog = I2C_V3_SELECT | (u8) selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	ret = i2c_smbus_write_byte(client, v3_prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	max1586->v3_curr_sel = selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return 0;
^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) static int max1586_v6_get_voltage_sel(struct regulator_dev *rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return max1586->v6_curr_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static int max1586_v6_set_voltage_sel(struct regulator_dev *rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 				      unsigned int selector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct max1586_data *max1586 = rdev_get_drvdata(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct i2c_client *client = max1586->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u8 v6_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	dev_dbg(&client->dev, "changing voltage v6 to %dmv\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		rdev->desc->volt_table[selector] / 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	v6_prog = I2C_V6_SELECT | (u8) selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	ret = i2c_smbus_write_byte(client, v6_prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	max1586->v6_curr_sel = selector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * The Maxim 1586 controls V3 and V6 voltages, but offers no way of reading back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * the set up value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static const struct regulator_ops max1586_v3_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	.get_voltage_sel = max1586_v3_get_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	.set_voltage_sel = max1586_v3_set_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	.list_voltage = regulator_list_voltage_linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	.map_voltage = regulator_map_voltage_linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static const struct regulator_ops max1586_v6_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	.get_voltage_sel = max1586_v6_get_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.set_voltage_sel = max1586_v6_set_voltage_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	.list_voltage = regulator_list_voltage_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static struct regulator_desc max1586_reg[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		.name = "Output_V3",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		.id = MAX1586_V3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		.ops = &max1586_v3_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		.type = REGULATOR_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		.n_voltages = MAX1586_V3_MAX_VSEL + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		.name = "Output_V6",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		.id = MAX1586_V6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		.ops = &max1586_v6_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		.type = REGULATOR_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		.n_voltages = MAX1586_V6_MAX_VSEL + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		.volt_table = v6_voltages_uv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int of_get_max1586_platform_data(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				 struct max1586_platform_data *pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct max1586_subdev_data *sub;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct of_regulator_match rmatch[ARRAY_SIZE(max1586_reg)] = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int i, matched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (of_property_read_u32(np, "v3-gain",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				 &pdata->v3_gain) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		dev_err(dev, "%pOF has no 'v3-gain' property\n", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	np = of_get_child_by_name(np, "regulators");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		dev_err(dev, "missing 'regulators' subnode in DT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	for (i = 0; i < ARRAY_SIZE(rmatch); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		rmatch[i].name = max1586_reg[i].name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 * If matched is 0, ie. neither Output_V3 nor Output_V6 have been found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 * return 0, which signals the normal situation where no subregulator is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	 * available. This is normal because the max1586 doesn't provide any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * readback support, so the subregulators can't report any status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * anyway.  If matched < 0, return the error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (matched <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return matched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	pdata->subdevs = devm_kcalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				      matched,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				      sizeof(struct max1586_subdev_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (!pdata->subdevs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	pdata->num_subdevs = matched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	sub = pdata->subdevs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	for (i = 0; i < matched; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		sub->id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		sub->name = rmatch[i].of_node->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		sub->platform_data = rmatch[i].init_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		sub++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static const struct of_device_id __maybe_unused max1586_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	{ .compatible = "maxim,max1586", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) MODULE_DEVICE_TABLE(of, max1586_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int max1586_pmic_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 					const struct i2c_device_id *i2c_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct max1586_platform_data *pdata, pdata_of;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	struct regulator_config config = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct max1586_data *max1586;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	int i, id, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	const struct of_device_id *match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	pdata = dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (client->dev.of_node && !pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		match = of_match_device(of_match_ptr(max1586_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 					&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		if (!match) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			dev_err(&client->dev, "Error: No device match found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		ret = of_get_max1586_platform_data(&client->dev, &pdata_of);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		pdata = &pdata_of;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	max1586 = devm_kzalloc(&client->dev, sizeof(struct max1586_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (!max1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	max1586->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (!pdata->v3_gain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	max1586->min_uV = MAX1586_V3_MIN_UV / 1000 * pdata->v3_gain / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	max1586->max_uV = MAX1586_V3_MAX_UV / 1000 * pdata->v3_gain / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	/* Set curr_sel to default voltage on power-up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	max1586->v3_curr_sel = 24; /* 1.3V */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	max1586->v6_curr_sel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	for (i = 0; i < pdata->num_subdevs && i <= MAX1586_V6; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		struct regulator_dev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		id = pdata->subdevs[i].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		if (!pdata->subdevs[i].platform_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (id < MAX1586_V3 || id > MAX1586_V6) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			dev_err(&client->dev, "invalid regulator id %d\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (id == MAX1586_V3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			max1586_reg[id].min_uV = max1586->min_uV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			max1586_reg[id].uV_step =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 					(max1586->max_uV - max1586->min_uV) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 					MAX1586_V3_MAX_VSEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		config.dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		config.init_data = pdata->subdevs[i].platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		config.driver_data = max1586;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		rdev = devm_regulator_register(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 						  &max1586_reg[id], &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		if (IS_ERR(rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			dev_err(&client->dev, "failed to register %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				max1586_reg[id].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			return PTR_ERR(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	i2c_set_clientdata(client, max1586);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	dev_info(&client->dev, "Maxim 1586 regulator driver loaded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static const struct i2c_device_id max1586_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	{ "max1586", 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) MODULE_DEVICE_TABLE(i2c, max1586_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static struct i2c_driver max1586_pmic_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.probe = max1586_pmic_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		.name	= "max1586",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		.of_match_table = of_match_ptr(max1586_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	.id_table	= max1586_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int __init max1586_pmic_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return i2c_add_driver(&max1586_pmic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) subsys_initcall(max1586_pmic_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static void __exit max1586_pmic_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	i2c_del_driver(&max1586_pmic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) module_exit(max1586_pmic_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* Module information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) MODULE_DESCRIPTION("MAXIM 1586 voltage regulator driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) MODULE_AUTHOR("Robert Jarzmik");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) MODULE_LICENSE("GPL");