blob: b54b573eeda5770af0bab91f9b45720abcf9db9c [file] [log] [blame]
Jan Kundrát25016432019-03-04 21:40:58 +01001diff --git a/Documentation/devicetree/bindings/pinctrl/pinctrl-mcp23s08.txt b/Documentation/devicetree/bindings/pinctrl/pinctrl-mcp23s08.txt
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01002index 6ec3c8d79f49..80acd6849648 100644
Jan Kundrát25016432019-03-04 21:40:58 +01003--- a/Documentation/devicetree/bindings/pinctrl/pinctrl-mcp23s08.txt
4+++ b/Documentation/devicetree/bindings/pinctrl/pinctrl-mcp23s08.txt
5@@ -144,3 +144,38 @@ gpio21: gpio@21 {
6 bias-pull-up;
7 };
8 };
9+
10+Line naming
11+===========
12+
13+Because several gpio_chip instances are hidden below a single device tree
14+node, it is necessary to split the names into several child nodes. Ensure
15+that the configured addresses match those in the microchip,spi-present-mask:
16+
17+gpio@0 {
Jan Kundráte7d21622019-03-28 22:52:38 +010018+ compatible = "microchip,mcp23s17";
19+ gpio-controller;
20+ #gpio-cells = <2>;
Jan Kundrát25016432019-03-04 21:40:58 +010021+ /* this bitmask has bits #0 (0x01) and #2 (0x04) set */
Jan Kundráte7d21622019-03-28 22:52:38 +010022+ spi-present-mask = <0x05>;
23+ reg = <0>;
24+ spi-max-frequency = <1000000>;
Jan Kundrát25016432019-03-04 21:40:58 +010025+
26+ gpio-bank@1 {
27+ address = <0>;
28+ gpio-line-names =
29+ "GPA0",
30+ "GPA1",
31+ ...
32+ "GPA7",
33+ "GPB0",
34+ "GPB1",
35+ ...
36+ "GPB7";
37+ };
38+
39+ gpio-bank@2 {
40+ address = <2>;
41+ gpio-line-names = ...
42+ };
43+};
Václav Kubernátf1b92ff2021-03-24 07:41:23 +010044diff --git a/Documentation/hwmon/fsp-3y.rst b/Documentation/hwmon/fsp-3y.rst
45new file mode 100644
46index 000000000000..5693d83a2035
47--- /dev/null
48+++ b/Documentation/hwmon/fsp-3y.rst
49@@ -0,0 +1,28 @@
50+.. SPDX-License-Identifier: GPL-2.0
51+
52+Kernel driver fsp3y
53+======================
54+Supported devices:
55+ * 3Y POWER YH-5151E
56+ * 3Y POWER YM-2151E
57+
58+Author: Václav Kubernát <kubernat@cesnet.cz>
59+
60+Description
61+-----------
62+This driver implements limited support for two 3Y POWER devices.
63+
64+Sysfs entries
65+-------------
66+ * in1_input input voltage
67+ * in2_input 12V output voltage
68+ * in3_input 5V output voltage
69+ * curr1_input input current
70+ * curr2_input 12V output current
71+ * curr3_input 5V output current
72+ * fan1_input fan rpm
73+ * temp1_input temperature 1
74+ * temp2_input temperature 2
75+ * temp3_input temperature 3
76+ * power1_input input power
77+ * power2_input output power
78diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst
79index 8d5a2df1ecb6..ed32c0f200bb 100644
80--- a/Documentation/hwmon/index.rst
81+++ b/Documentation/hwmon/index.rst
82@@ -62,6 +62,7 @@ Hardware Monitoring Kernel Drivers
83 f71805f
84 f71882fg
85 fam15h_power
86+ fsp-3y
87 ftsteutates
88 g760a
89 g762
Jan Kundrátbf1e0912021-03-05 12:45:23 +010090diff --git a/Documentation/hwmon/max31790.rst b/Documentation/hwmon/max31790.rst
Václav Kubernátf1b92ff2021-03-24 07:41:23 +010091index f301385d8cef..e70aae9f71a2 100644
Jan Kundrátbf1e0912021-03-05 12:45:23 +010092--- a/Documentation/hwmon/max31790.rst
93+++ b/Documentation/hwmon/max31790.rst
Václav Kubernátf1b92ff2021-03-24 07:41:23 +010094@@ -30,14 +30,58 @@ monitoring and control of fan RPM as well as detection of fan failure.
Jan Kundrátbf1e0912021-03-05 12:45:23 +010095 Six pins are dedicated tachometer inputs. Any of the six PWM outputs can
96 also be configured to serve as tachometer inputs.
97
98+About pwm[1-6]_enable
99+---------------------
100+0 - full-speed
101+ The chip doesn't have a specific way to set "full speed", so setting
102+ pwm[1-6]_enable to 0 is just "set PWM mode with 255 duty cycle".
103+1 - PWM mode
104+ Fan speed is controlled by writing a value to pwm[1-6].
105+2 - RPM mode
106+ Fan speed is controlled by writing a value to fan[1-6]_target.
107+
108+About fan[1-6]_fault
109+--------------------
110+In PWM (or full-speed) mode, if the input RPM goes below what is set
111+in fan[1-6]_target, fan[1-6]_fault gets set to 1. In other words,
112+fan[1-6]_target works as the minimum input RPM before a fan fault goes off.
113+
114+In RPM mode, fan fault is set when the fan spins "too slowly" (exact
115+conditions are in the datasheet). RPM mode depends on four variables:
116+ target_speed: This is set by fan[1-6]_target.
117+ speed_range: This is set automatically when setting target_speed
118+ or manually by fan[1-12]_div.
119+ pwm_rate_of_change: NOT set by the driver.
120+ fan_window: NOT set by the driver.
121+
122+The last two values are not set by the driver, because there's no generic way to
123+compute them. You should set them manually through i2c (in the bootloader for
124+example). Check the datasheet for details.
125+
126+The fan fault value latches, to reset it, set a value to pwm[1-6]
127+or fan[1-6]_target.
128+
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100129+About fan[1-12]_div
130+-------------------
131+This value affects the measurable range of the chip. The driver sets this value
132+automatically in RPM based on fan[1-6]_target. In PWM mode, you should set this
133+value manually based on the details from the datasheet. Setting the speed range
134+is disabled while in RPM mode to prevent overwriting the automatically
135+calculated value.
136
137 Sysfs entries
138 -------------
139
140-================== === =======================================================
141+================== === =============================================================
142+fan[1-12]_enable RW enable fan speed monitoring
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100143 fan[1-12]_input RO fan tachometer speed in RPM
144 fan[1-12]_fault RO fan experienced fault
145-fan[1-6]_target RW desired fan speed in RPM
146-pwm[1-6]_enable RW regulator mode, 0=disabled, 1=manual mode, 2=rpm mode
147+fan[1-12]_div RW set the measurable speed range, not available in RPM mode
148+fan[1-6]_target RW RPM mode = desired fan speed
149+ PWM mode = minimum fan speed until fault
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100150+pwm[1-6]_enable RW regulator mode, 0=no control, sets 0% PWM,
151+ 1=manual (pwm) mode,
152+ 2=rpm mode
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100153+ setting rpm mode sets fan*_enable to 1
154 pwm[1-6] RW fan target duty cycle (0-255)
155-================== === =======================================================
156+================== === =============================================================
157diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100158index 54f04e61fb83..c2ec57672c4e 100644
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100159--- a/drivers/hwmon/Kconfig
160+++ b/drivers/hwmon/Kconfig
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100161@@ -1092,6 +1092,7 @@ config SENSORS_MAX6697
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100162 config SENSORS_MAX31790
163 tristate "Maxim MAX31790 sensor chip"
164 depends on I2C
165+ select REGMAP_I2C
166 help
167 If you say yes here you get support for 6-Channel PWM-Output
168 Fan RPM Controller.
Jan Kundrát6406d512020-11-18 18:24:36 +0100169diff --git a/drivers/hwmon/max31790.c b/drivers/hwmon/max31790.c
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100170index 86e6c71db685..8b04f4c01752 100644
Jan Kundrát6406d512020-11-18 18:24:36 +0100171--- a/drivers/hwmon/max31790.c
172+++ b/drivers/hwmon/max31790.c
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100173@@ -12,6 +12,7 @@
174 #include <linux/init.h>
175 #include <linux/jiffies.h>
176 #include <linux/module.h>
177+#include <linux/regmap.h>
178 #include <linux/slab.h>
179
180 /* MAX31790 registers */
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100181@@ -29,6 +30,7 @@
182 #define MAX31790_FAN_CFG_RPM_MODE 0x80
183 #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08
184 #define MAX31790_FAN_CFG_TACH_INPUT 0x01
185+#define MAX31790_FAN_CFG_CTRL_MON 0x10
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100186
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100187 /* Fan Dynamics register bits */
188 #define MAX31790_FAN_DYN_SR_SHIFT 5
189@@ -46,92 +48,53 @@
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100190
191 #define NR_CHANNEL 6
192
193+#define MAX31790_REG_USER_BYTE_67 0x67
194+
195+#define BULK_TO_U16(msb, lsb) (((msb) << 8) + (lsb))
196+#define U16_MSB(num) (((num) & 0xFF00) >> 8)
197+#define U16_LSB(num) ((num) & 0x00FF)
198+
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100199+static const struct regmap_range max31790_ro_range = {
200+ .range_min = MAX31790_REG_TACH_COUNT(0),
201+ .range_max = MAX31790_REG_PWMOUT(0) - 1,
202+};
203+
204+static const struct regmap_access_table max31790_wr_table = {
205+ .no_ranges = &max31790_ro_range,
206+ .n_no_ranges = 1,
207+};
208+
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100209+static const struct regmap_range max31790_volatile_ranges[] = {
210+ regmap_reg_range(MAX31790_REG_TACH_COUNT(0), MAX31790_REG_TACH_COUNT(12)),
211+ regmap_reg_range(MAX31790_REG_FAN_FAULT_STATUS2, MAX31790_REG_FAN_FAULT_STATUS1),
212+};
213+
214+static const struct regmap_access_table max31790_volatile_table = {
215+ .no_ranges = max31790_volatile_ranges,
216+ .n_no_ranges = 2,
217+ .n_yes_ranges = 0
218+};
219+
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100220+static const struct regmap_config max31790_regmap_config = {
221+ .reg_bits = 8,
222+ .val_bits = 8,
223+ .reg_stride = 1,
224+ .max_register = MAX31790_REG_USER_BYTE_67,
225+ .wr_table = &max31790_wr_table,
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100226+ .volatile_table = &max31790_volatile_table
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100227+};
228+
229 /*
230 * Client data (each client gets its own)
231 */
232 struct max31790_data {
233- struct i2c_client *client;
234+ struct regmap *regmap;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100235+
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100236 struct mutex update_lock;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100237- bool valid; /* zero until following fields are valid */
238- unsigned long last_updated; /* in jiffies */
239-
240- /* register values */
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100241 u8 fan_config[NR_CHANNEL];
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100242 u8 fan_dynamics[NR_CHANNEL];
243- u16 fault_status;
244- u16 tach[NR_CHANNEL * 2];
245- u16 pwm[NR_CHANNEL];
246- u16 target_count[NR_CHANNEL];
247 };
248
249-static struct max31790_data *max31790_update_device(struct device *dev)
250-{
251- struct max31790_data *data = dev_get_drvdata(dev);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100252- struct i2c_client *client = data->client;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100253- struct max31790_data *ret = data;
254- int i;
255- int rv;
256-
257- mutex_lock(&data->update_lock);
258-
259- if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100260- rv = i2c_smbus_read_byte_data(client,
261- MAX31790_REG_FAN_FAULT_STATUS1);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100262- if (rv < 0)
263- goto abort;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100264- data->fault_status = rv & 0x3F;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100265-
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100266- rv = i2c_smbus_read_byte_data(client,
267- MAX31790_REG_FAN_FAULT_STATUS2);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100268- if (rv < 0)
269- goto abort;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100270- data->fault_status |= (rv & 0x3F) << 6;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100271-
272- for (i = 0; i < NR_CHANNEL; i++) {
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100273- rv = i2c_smbus_read_word_swapped(client,
274- MAX31790_REG_TACH_COUNT(i));
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100275- if (rv < 0)
276- goto abort;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100277- data->tach[i] = rv;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100278-
279- if (data->fan_config[i]
280- & MAX31790_FAN_CFG_TACH_INPUT) {
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100281- rv = i2c_smbus_read_word_swapped(client,
282- MAX31790_REG_TACH_COUNT(NR_CHANNEL
283- + i));
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100284- if (rv < 0)
285- goto abort;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100286- data->tach[NR_CHANNEL + i] = rv;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100287- } else {
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100288- rv = i2c_smbus_read_word_swapped(client,
289- MAX31790_REG_PWMOUT(i));
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100290- if (rv < 0)
291- goto abort;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100292- data->pwm[i] = rv;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100293-
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100294- rv = i2c_smbus_read_word_swapped(client,
295- MAX31790_REG_TARGET_COUNT(i));
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100296- if (rv < 0)
297- goto abort;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100298- data->target_count[i] = rv;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100299- }
300- }
301-
302- data->last_updated = jiffies;
303- data->valid = true;
304- }
305- goto done;
306-
307-abort:
308- data->valid = false;
309- ret = ERR_PTR(rv);
310-
311-done:
312- mutex_unlock(&data->update_lock);
313-
314- return ret;
315-}
316-
317 static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100318
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100319 static u8 get_tach_period(u8 fan_dynamics)
320@@ -159,28 +122,109 @@ static u8 bits_for_tach_period(int rpm)
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100321 return bits;
322 }
323
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100324+static int read_reg_byte(struct regmap *regmap, u8 reg)
325+{
326+ int rv;
327+ int val;
328+
329+ rv = regmap_read(regmap, reg, &val);
330+ if (rv < 0)
331+ return rv;
332+
333+ return val;
334+}
335+
336+static int read_reg_word(struct regmap *regmap, u8 reg)
337+{
338+ int rv;
339+ u8 val_bulk[2];
340+
341+ rv = regmap_bulk_read(regmap, reg, val_bulk, 2);
342+ if (rv < 0)
343+ return rv;
344+
345+ return BULK_TO_U16(val_bulk[0], val_bulk[1]);
346+}
347+
348+static int write_reg_word(struct regmap *regmap, u8 reg, u16 val)
349+{
350+ u8 bulk_val[2];
351+
352+ bulk_val[0] = U16_MSB(val);
353+ bulk_val[1] = U16_LSB(val);
354+
355+ return regmap_bulk_write(regmap, reg, bulk_val, 2);
356+}
357+
358+static int bits_for_speed_range(long speed_range)
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100359+{
360+ switch (speed_range) {
361+ case 1:
362+ return 0x0;
363+ case 2:
364+ return 0x1;
365+ case 4:
366+ return 0x2;
367+ case 8:
368+ return 0x3;
369+ case 16:
370+ return 0x4;
371+ case 32:
372+ return 0x5;
373+ default:
374+ return -1;
375+ }
376+}
377+
378 static int max31790_read_fan(struct device *dev, u32 attr, int channel,
379 long *val)
380 {
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100381- struct max31790_data *data = max31790_update_device(dev);
382- int sr, rpm;
383-
384- if (IS_ERR(data))
385- return PTR_ERR(data);
386+ struct max31790_data *data = dev_get_drvdata(dev);
387+ struct regmap *regmap = data->regmap;
388+ int tach, fault;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100389
390 switch (attr) {
391 case hwmon_fan_input:
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100392- sr = get_tach_period(data->fan_dynamics[channel]);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100393- rpm = RPM_FROM_REG(data->tach[channel], sr);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100394- *val = rpm;
395+ if (!(data->fan_config[channel] & MAX31790_FAN_CFG_TACH_INPUT_EN))
396+ return -ENODATA;
397+
398+ tach = read_reg_word(regmap, MAX31790_REG_TACH_COUNT(channel));
399+ if (tach < 0)
400+ return tach;
401+
402+ *val = RPM_FROM_REG(tach, get_tach_period(data->fan_dynamics[channel]));
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100403 return 0;
404 case hwmon_fan_target:
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100405- sr = get_tach_period(data->fan_dynamics[channel]);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100406- rpm = RPM_FROM_REG(data->target_count[channel], sr);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100407- *val = rpm;
408+ tach = read_reg_word(regmap, MAX31790_REG_TARGET_COUNT(channel));
409+ if (tach < 0)
410+ return tach;
411+
412+ *val = RPM_FROM_REG(tach, get_tach_period(data->fan_dynamics[channel]));
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100413 return 0;
414 case hwmon_fan_fault:
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100415- *val = !!(data->fault_status & (1 << channel));
416+ if (!(data->fan_config[channel] & MAX31790_FAN_CFG_TACH_INPUT_EN)) {
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100417+ *val = 0;
418+ return 0;
419+ }
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100420+
421+ if (channel > 6)
422+ fault = read_reg_byte(regmap, MAX31790_REG_FAN_FAULT_STATUS2);
423+ else
424+ fault = read_reg_byte(regmap, MAX31790_REG_FAN_FAULT_STATUS1);
425+
426+ if (fault < 0)
427+ return fault;
428+
429+ if (channel > 6)
430+ *val = !!(fault & (1 << (channel - 6)));
431+ else
432+ *val = !!(fault & (1 << channel));
433+ return 0;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100434+ case hwmon_fan_enable:
435+ *val = !!(data->fan_config[channel] & MAX31790_FAN_CFG_TACH_INPUT_EN);
436+ return 0;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100437+ case hwmon_fan_div:
438+ *val = get_tach_period(data->fan_config[channel]);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100439 return 0;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100440 default:
441 return -EOPNOTSUPP;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100442@@ -191,7 +235,7 @@ static int max31790_write_fan(struct device *dev, u32 attr, int channel,
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100443 long val)
444 {
445 struct max31790_data *data = dev_get_drvdata(dev);
446- struct i2c_client *client = data->client;
447+ struct regmap *regmap = data->regmap;
448 int target_count;
449 int err = 0;
450 u8 bits;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100451@@ -207,9 +251,10 @@ static int max31790_write_fan(struct device *dev, u32 attr, int channel,
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100452 ((data->fan_dynamics[channel] &
453 ~MAX31790_FAN_DYN_SR_MASK) |
454 (bits << MAX31790_FAN_DYN_SR_SHIFT));
455- err = i2c_smbus_write_byte_data(client,
456- MAX31790_REG_FAN_DYNAMICS(channel),
457- data->fan_dynamics[channel]);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100458+
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100459+ err = regmap_write(regmap,
460+ MAX31790_REG_FAN_DYNAMICS(channel),
461+ data->fan_dynamics[channel]);
462 if (err < 0)
463 break;
464
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100465@@ -217,11 +262,38 @@ static int max31790_write_fan(struct device *dev, u32 attr, int channel,
466 target_count = RPM_TO_REG(val, sr);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100467 target_count = clamp_val(target_count, 0x1, 0x7FF);
468
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100469- data->target_count[channel] = target_count << 5;
470+ target_count = target_count << 5;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100471
472- err = i2c_smbus_write_word_swapped(client,
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100473- MAX31790_REG_TARGET_COUNT(channel),
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100474- data->target_count[channel]);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100475+ err = write_reg_word(regmap,
476+ MAX31790_REG_TARGET_COUNT(channel),
477+ target_count);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100478+ break;
479+ case hwmon_fan_enable:
480+ if (val == 0)
481+ data->fan_config[channel] &= ~MAX31790_FAN_CFG_TACH_INPUT_EN;
482+ else
483+ data->fan_config[channel] |= MAX31790_FAN_CFG_TACH_INPUT_EN;
484+ err = regmap_write(regmap,
485+ MAX31790_REG_FAN_CONFIG(channel),
486+ data->fan_config[channel]);
487+ break;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100488+ case hwmon_fan_div:
489+ if (data->fan_config[channel] & MAX31790_FAN_CFG_RPM_MODE) {
490+ err = -EINVAL;
491+ break;
492+ }
493+ sr = bits_for_speed_range(val);
494+ if (sr < 0) {
495+ err = -EINVAL;
496+ break;
497+ }
498+
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100499+ data->fan_dynamics[channel] = ((data->fan_dynamics[channel] &
500+ ~MAX31790_FAN_DYN_SR_MASK) |
501+ (sr << MAX31790_FAN_DYN_SR_SHIFT));
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100502+ err = regmap_write(regmap,
503+ MAX31790_REG_FAN_DYNAMICS(channel),
504+ data->fan_dynamics[channel]);
505 break;
506 default:
507 err = -EOPNOTSUPP;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100508@@ -250,6 +322,12 @@ static umode_t max31790_fan_is_visible(const void *_data, u32 attr, int channel)
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100509 !(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
510 return 0644;
511 return 0;
512+ case hwmon_fan_enable:
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100513+ case hwmon_fan_div:
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100514+ if (channel < NR_CHANNEL ||
515+ (fan_config & MAX31790_FAN_CFG_TACH_INPUT))
516+ return 0644;
517+ return 0;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100518 default:
519 return 0;
520 }
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100521@@ -258,25 +336,25 @@ static umode_t max31790_fan_is_visible(const void *_data, u32 attr, int channel)
522 static int max31790_read_pwm(struct device *dev, u32 attr, int channel,
523 long *val)
524 {
525- struct max31790_data *data = max31790_update_device(dev);
526- u8 fan_config;
527-
528- if (IS_ERR(data))
529- return PTR_ERR(data);
530-
531- fan_config = data->fan_config[channel];
532+ struct max31790_data *data = dev_get_drvdata(dev);
533+ struct regmap *regmap = data->regmap;
534+ int read;
535
536 switch (attr) {
537 case hwmon_pwm_input:
538- *val = data->pwm[channel] >> 8;
539+ read = read_reg_word(regmap, MAX31790_REG_PWMOUT(channel));
540+ if (read < 0)
541+ return read;
542+
543+ *val = read >> 8;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100544 return 0;
545 case hwmon_pwm_enable:
546- if (fan_config & MAX31790_FAN_CFG_RPM_MODE)
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100547+ if (data->fan_config[channel] & MAX31790_FAN_CFG_CTRL_MON)
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100548+ *val = 0;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100549+ else if (data->fan_config[channel] & MAX31790_FAN_CFG_RPM_MODE)
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100550 *val = 2;
551- else if (fan_config & MAX31790_FAN_CFG_TACH_INPUT_EN)
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100552+ else
553 *val = 1;
554- else
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100555- *val = 0;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100556 return 0;
557 default:
558 return -EOPNOTSUPP;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100559@@ -287,43 +365,46 @@ static int max31790_write_pwm(struct device *dev, u32 attr, int channel,
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100560 long val)
561 {
562 struct max31790_data *data = dev_get_drvdata(dev);
563- struct i2c_client *client = data->client;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100564- u8 fan_config;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100565+ struct regmap *regmap = data->regmap;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100566+ u8 fan_config = data->fan_config[channel];
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100567 int err = 0;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100568
569 mutex_lock(&data->update_lock);
570
571 switch (attr) {
572 case hwmon_pwm_input:
573- if (val < 0 || val > 255) {
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100574+ if (fan_config & MAX31790_FAN_CFG_CTRL_MON || val < 0 || val > 255) {
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100575 err = -EINVAL;
576 break;
577 }
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100578- data->pwm[channel] = val << 8;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100579- err = i2c_smbus_write_word_swapped(client,
580- MAX31790_REG_PWMOUT(channel),
581- data->pwm[channel]);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100582+
583+ err = write_reg_word(regmap, MAX31790_REG_PWMOUT(channel), val << 8);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100584 break;
585 case hwmon_pwm_enable:
586 fan_config = data->fan_config[channel];
587- if (val == 0) {
588- fan_config &= ~(MAX31790_FAN_CFG_TACH_INPUT_EN |
589- MAX31790_FAN_CFG_RPM_MODE);
590- } else if (val == 1) {
591- fan_config = (fan_config |
592- MAX31790_FAN_CFG_TACH_INPUT_EN) &
593- ~MAX31790_FAN_CFG_RPM_MODE;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100594+ if (val == 0)
595+ fan_config |= MAX31790_FAN_CFG_CTRL_MON;
596+ else if (val == 1) {
597+ fan_config &= ~(MAX31790_FAN_CFG_RPM_MODE | MAX31790_FAN_CFG_CTRL_MON);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100598 } else if (val == 2) {
599- fan_config |= MAX31790_FAN_CFG_TACH_INPUT_EN |
600- MAX31790_FAN_CFG_RPM_MODE;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100601+ fan_config &= ~(MAX31790_FAN_CFG_CTRL_MON);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100602+ fan_config |= MAX31790_FAN_CFG_RPM_MODE;
603 } else {
604 err = -EINVAL;
605 break;
606 }
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100607+
608+ /*
609+ * RPM mode implies enabled TACH input, so enable it in RPM
610+ * mode.
611+ */
612+ if (val == 2)
613+ fan_config |= MAX31790_FAN_CFG_TACH_INPUT_EN;
614+
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100615 data->fan_config[channel] = fan_config;
616- err = i2c_smbus_write_byte_data(client,
617- MAX31790_REG_FAN_CONFIG(channel),
618- fan_config);
619+ err = regmap_write(regmap,
620+ MAX31790_REG_FAN_CONFIG(channel),
621+ fan_config);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100622 break;
623 default:
624 err = -EOPNOTSUPP;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100625@@ -393,18 +474,18 @@ static umode_t max31790_is_visible(const void *data,
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100626
627 static const struct hwmon_channel_info *max31790_info[] = {
628 HWMON_CHANNEL_INFO(fan,
629- HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
630- HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
631- HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
632- HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
633- HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
634- HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
635- HWMON_F_INPUT | HWMON_F_FAULT,
636- HWMON_F_INPUT | HWMON_F_FAULT,
637- HWMON_F_INPUT | HWMON_F_FAULT,
638- HWMON_F_INPUT | HWMON_F_FAULT,
639- HWMON_F_INPUT | HWMON_F_FAULT,
640- HWMON_F_INPUT | HWMON_F_FAULT),
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100641+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
642+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
643+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
644+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
645+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
646+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_FAULT,
647+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
648+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
649+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
650+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
651+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT,
652+ HWMON_F_DIV | HWMON_F_ENABLE | HWMON_F_INPUT | HWMON_F_FAULT),
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100653 HWMON_CHANNEL_INFO(pwm,
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100654 HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
655 HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
656@@ -426,20 +507,18 @@ static const struct hwmon_chip_info max31790_chip_info = {
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100657 .info = max31790_info,
658 };
659
660-static int max31790_init_client(struct i2c_client *client,
661+static int max31790_init_client(struct regmap *regmap,
662 struct max31790_data *data)
663 {
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100664 int i, rv;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100665
666 for (i = 0; i < NR_CHANNEL; i++) {
667- rv = i2c_smbus_read_byte_data(client,
668- MAX31790_REG_FAN_CONFIG(i));
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100669+ rv = read_reg_byte(regmap, MAX31790_REG_FAN_CONFIG(i % NR_CHANNEL));
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100670 if (rv < 0)
671 return rv;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100672 data->fan_config[i] = rv;
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100673
674- rv = i2c_smbus_read_byte_data(client,
675- MAX31790_REG_FAN_DYNAMICS(i));
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100676+ rv = read_reg_byte(regmap, MAX31790_REG_FAN_DYNAMICS(i));
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100677 if (rv < 0)
678 return rv;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100679 data->fan_dynamics[i] = rv;
680@@ -464,13 +543,18 @@ static int max31790_probe(struct i2c_client *client)
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100681 if (!data)
682 return -ENOMEM;
683
684- data->client = client;
685 mutex_init(&data->update_lock);
686
687+ data->regmap = devm_regmap_init_i2c(client, &max31790_regmap_config);
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100688+ if (IS_ERR(data->regmap)) {
689+ dev_err(dev, "failed to allocate register map\n");
690+ return PTR_ERR(data->regmap);
691+ }
692+
693 /*
694 * Initialize the max31790 chip
695 */
696- err = max31790_init_client(client, data);
697+ err = max31790_init_client(data->regmap, data);
698 if (err)
699 return err;
700
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100701@@ -488,11 +572,18 @@ static const struct i2c_device_id max31790_id[] = {
Jan Kundrát6406d512020-11-18 18:24:36 +0100702 };
703 MODULE_DEVICE_TABLE(i2c, max31790_id);
704
705+static const struct of_device_id __maybe_unused max31790_of_match[] = {
706+ { .compatible = "maxim,max31790" },
707+ { }
708+};
709+MODULE_DEVICE_TABLE(of, max31790_of_match);
710+
711 static struct i2c_driver max31790_driver = {
712 .class = I2C_CLASS_HWMON,
Jan Kundrátbf1e0912021-03-05 12:45:23 +0100713 .probe_new = max31790_probe,
Jan Kundrát6406d512020-11-18 18:24:36 +0100714 .driver = {
715 .name = "max31790",
716+ .of_match_table = of_match_ptr(max31790_of_match),
717 },
718 .id_table = max31790_id,
719 };
Václav Kubernátf1b92ff2021-03-24 07:41:23 +0100720diff --git a/drivers/hwmon/pmbus/Kconfig b/drivers/hwmon/pmbus/Kconfig
721index 32d2fc850621..5ec5b3bd1986 100644
722--- a/drivers/hwmon/pmbus/Kconfig
723+++ b/drivers/hwmon/pmbus/Kconfig
724@@ -56,6 +56,16 @@ config SENSORS_BEL_PFE
725 This driver can also be built as a module. If so, the module will
726 be called bel-pfe.
727
728+config SENSORS_FSP_3Y
729+ tristate "FSP/3Y-Power power supplies"
730+ help
731+ If you say yes here you get hardware monitoring support for
732+ FSP/3Y-Power hot-swap power supplies.
733+ Supported models: YH-5151E, YM-2151E
734+
735+ This driver can also be built as a module. If so, the module will
736+ be called fsp-3y.
737+
738 config SENSORS_IBM_CFFPS
739 tristate "IBM Common Form Factor Power Supply"
740 depends on LEDS_CLASS
741diff --git a/drivers/hwmon/pmbus/Makefile b/drivers/hwmon/pmbus/Makefile
742index 6a4ba0fdc1db..bfe218ad898f 100644
743--- a/drivers/hwmon/pmbus/Makefile
744+++ b/drivers/hwmon/pmbus/Makefile
745@@ -8,6 +8,7 @@ obj-$(CONFIG_SENSORS_PMBUS) += pmbus.o
746 obj-$(CONFIG_SENSORS_ADM1266) += adm1266.o
747 obj-$(CONFIG_SENSORS_ADM1275) += adm1275.o
748 obj-$(CONFIG_SENSORS_BEL_PFE) += bel-pfe.o
749+obj-$(CONFIG_SENSORS_FSP_3Y) += fsp-3y.o
750 obj-$(CONFIG_SENSORS_IBM_CFFPS) += ibm-cffps.o
751 obj-$(CONFIG_SENSORS_INSPUR_IPSPS) += inspur-ipsps.o
752 obj-$(CONFIG_SENSORS_IR35221) += ir35221.o
753diff --git a/drivers/hwmon/pmbus/fsp-3y.c b/drivers/hwmon/pmbus/fsp-3y.c
754new file mode 100644
755index 000000000000..073d960a0af5
756--- /dev/null
757+++ b/drivers/hwmon/pmbus/fsp-3y.c
758@@ -0,0 +1,294 @@
759+// SPDX-License-Identifier: GPL-2.0-or-later
760+/*
761+ * Hardware monitoring driver for FSP 3Y-Power PSUs
762+ *
763+ * Copyright (c) 2021 Václav Kubernát, CESNET
764+ *
765+ * This driver is mostly reverse engineered with the help of a tool called pmbus_peek written by
766+ * David Brownell (and later adopted by Jan Kundrát). The device has some sort of a timing issue
767+ * when switching pages, details are explained in the code. The driver support is limited. It
768+ * exposes only the values, that have been tested to work correctly. Unsupported values either
769+ * aren't supported by the devices or their encondings are unknown.
770+ */
771+
772+#include <linux/delay.h>
773+#include <linux/i2c.h>
774+#include <linux/kernel.h>
775+#include <linux/module.h>
776+#include "pmbus.h"
777+
778+#define YM2151_PAGE_12V_LOG 0x00
779+#define YM2151_PAGE_12V_REAL 0x00
780+#define YM2151_PAGE_5VSB_LOG 0x01
781+#define YM2151_PAGE_5VSB_REAL 0x20
782+#define YH5151E_PAGE_12V_LOG 0x00
783+#define YH5151E_PAGE_12V_REAL 0x00
784+#define YH5151E_PAGE_5V_LOG 0x01
785+#define YH5151E_PAGE_5V_REAL 0x10
786+#define YH5151E_PAGE_3V3_LOG 0x02
787+#define YH5151E_PAGE_3V3_REAL 0x11
788+
789+enum chips {
790+ ym2151e,
791+ yh5151e
792+};
793+
794+struct fsp3y_data {
795+ struct pmbus_driver_info info;
796+ int chip;
797+ int page;
798+
799+ bool vout_linear_11;
800+};
801+
802+#define to_fsp3y_data(x) container_of(x, struct fsp3y_data, info)
803+
804+static int page_log_to_page_real(int page_log, enum chips chip)
805+{
806+ switch (chip) {
807+ case ym2151e:
808+ switch (page_log) {
809+ case YM2151_PAGE_12V_LOG:
810+ return YM2151_PAGE_12V_REAL;
811+ case YM2151_PAGE_5VSB_LOG:
812+ return YM2151_PAGE_5VSB_REAL;
813+ }
814+ return -EINVAL;
815+ case yh5151e:
816+ switch (page_log) {
817+ case YH5151E_PAGE_12V_LOG:
818+ return YH5151E_PAGE_12V_REAL;
819+ case YH5151E_PAGE_5V_LOG:
820+ return YH5151E_PAGE_5V_REAL;
821+ case YH5151E_PAGE_3V3_LOG:
822+ return YH5151E_PAGE_3V3_REAL;
823+ }
824+ return -EINVAL;
825+ }
826+
827+ return -EINVAL;
828+}
829+
830+static int set_page(struct i2c_client *client, int page_log)
831+{
832+ const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
833+ struct fsp3y_data *data = to_fsp3y_data(info);
834+ int rv;
835+ int page_real;
836+
837+ if (page_log < 0)
838+ return 0;
839+
840+ page_real = page_log_to_page_real(page_log, data->chip);
841+ if (page_real < 0)
842+ return page_real;
843+
844+ if (data->page != page_real) {
845+ rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page_real);
846+ if (rv < 0)
847+ return rv;
848+
849+ data->page = page_real;
850+
851+ /*
852+ * Testing showed that the device has a timing issue. After
853+ * setting a page, it takes a while, before the device actually
854+ * gives the correct values from the correct page. 20 ms was
855+ * tested to be enough to not give wrong values (15 ms wasn't
856+ * enough).
857+ */
858+ usleep_range(20000, 30000);
859+ }
860+
861+ return 0;
862+}
863+
864+static int fsp3y_read_byte_data(struct i2c_client *client, int page, int reg)
865+{
866+ const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
867+ struct fsp3y_data *data = to_fsp3y_data(info);
868+ int rv;
869+
870+ /*
871+ * Inject an exponent for non-compliant YH5151-E.
872+ */
873+ if (data->vout_linear_11 && reg == PMBUS_VOUT_MODE)
874+ return 0x1A;
875+
876+ rv = set_page(client, page);
877+ if (rv < 0)
878+ return rv;
879+
880+ return i2c_smbus_read_byte_data(client, reg);
881+}
882+
883+static int fsp3y_read_word_data(struct i2c_client *client, int page, int phase, int reg)
884+{
885+ const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
886+ struct fsp3y_data *data = to_fsp3y_data(info);
887+ int rv;
888+
889+ /*
890+ * This masks commands which weren't tested to work correctly. Some of
891+ * the masked commands return 0xFFFF. These would probably get tagged as
892+ * invalid by pmbus_core. Other ones do return values which might be
893+ * useful (that is, they are not 0xFFFF), but their encoding is unknown,
894+ * and so they are unsupported.
895+ */
896+ switch (reg) {
897+ case PMBUS_READ_FAN_SPEED_1:
898+ case PMBUS_READ_IIN:
899+ case PMBUS_READ_IOUT:
900+ case PMBUS_READ_PIN:
901+ case PMBUS_READ_POUT:
902+ case PMBUS_READ_TEMPERATURE_1:
903+ case PMBUS_READ_TEMPERATURE_2:
904+ case PMBUS_READ_TEMPERATURE_3:
905+ case PMBUS_READ_VIN:
906+ case PMBUS_READ_VOUT:
907+ case PMBUS_STATUS_WORD:
908+ break;
909+ default:
910+ return -ENXIO;
911+ }
912+
913+ rv = set_page(client, page);
914+ if (rv < 0)
915+ return rv;
916+
917+ rv = i2c_smbus_read_word_data(client, reg);
918+ if (rv < 0)
919+ return rv;
920+
921+ /*
922+ * Handle YH-5151E non-compliant linear11 vout voltage.
923+ */
924+ if (data->vout_linear_11 && reg == PMBUS_READ_VOUT)
925+ rv = sign_extend32(rv, 10) & 0xffff;
926+
927+ return rv;
928+}
929+
930+static struct pmbus_driver_info fsp3y_info[] = {
931+ [ym2151e] = {
932+ .pages = 2,
933+ .func[YM2151_PAGE_12V_LOG] =
934+ PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
935+ PMBUS_HAVE_PIN | PMBUS_HAVE_POUT |
936+ PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
937+ PMBUS_HAVE_VIN | PMBUS_HAVE_IIN |
938+ PMBUS_HAVE_FAN12,
939+ .func[YM2151_PAGE_5VSB_LOG] =
940+ PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT,
941+ PMBUS_HAVE_IIN,
942+ .read_word_data = fsp3y_read_word_data,
943+ .read_byte_data = fsp3y_read_byte_data,
944+ },
945+ [yh5151e] = {
946+ .pages = 3,
947+ .func[YH5151E_PAGE_12V_LOG] =
948+ PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
949+ PMBUS_HAVE_POUT |
950+ PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3,
951+ .func[YH5151E_PAGE_5V_LOG] =
952+ PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
953+ PMBUS_HAVE_POUT,
954+ .func[YH5151E_PAGE_3V3_LOG] =
955+ PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
956+ PMBUS_HAVE_POUT,
957+ .read_word_data = fsp3y_read_word_data,
958+ .read_byte_data = fsp3y_read_byte_data,
959+ }
960+};
961+
962+static int fsp3y_detect(struct i2c_client *client)
963+{
964+ int rv;
965+ u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
966+
967+ rv = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
968+ if (rv < 0)
969+ return rv;
970+
971+ buf[rv] = '\0';
972+
973+ if (rv == 8) {
974+ if (!strcmp(buf, "YM-2151E"))
975+ return ym2151e;
976+ else if (!strcmp(buf, "YH-5151E"))
977+ return yh5151e;
978+ }
979+
980+ dev_err(&client->dev, "Unsupported model %.*s\n", rv, buf);
981+ return -ENODEV;
982+}
983+
984+static const struct i2c_device_id fsp3y_id[] = {
985+ {"ym2151e", ym2151e},
986+ {"yh5151e", yh5151e},
987+ { }
988+};
989+
990+static int fsp3y_probe(struct i2c_client *client)
991+{
992+ struct fsp3y_data *data;
993+ const struct i2c_device_id *id;
994+ int rv;
995+
996+ data = devm_kzalloc(&client->dev, sizeof(struct fsp3y_data), GFP_KERNEL);
997+ if (!data)
998+ return -ENOMEM;
999+
1000+ data->chip = fsp3y_detect(client);
1001+ if (data->chip < 0)
1002+ return data->chip;
1003+
1004+ id = i2c_match_id(fsp3y_id, client);
1005+ if (data->chip != id->driver_data)
1006+ dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",
1007+ id->name, (int)id->driver_data, data->chip);
1008+
1009+ rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
1010+ if (rv < 0)
1011+ return rv;
1012+ data->page = rv;
1013+
1014+ data->info = fsp3y_info[data->chip];
1015+
1016+ /*
1017+ * YH-5151E sometimes reports vout in linear11 and sometimes in
1018+ * linear16. This depends on the exact individual piece of hardware. One
1019+ * YH-5151E can use linear16 and another might use linear11 instead.
1020+ *
1021+ * The format can be recognized by reading VOUT_MODE - if it doesn't
1022+ * report a valid exponent, then vout uses linear11. Otherwise, the
1023+ * device is compliant and uses linear16.
1024+ */
1025+ data->vout_linear_11 = false;
1026+ if (data->chip == yh5151e) {
1027+ rv = i2c_smbus_read_byte_data(client, PMBUS_VOUT_MODE);
1028+ if (rv < 0)
1029+ return rv;
1030+
1031+ if (rv == 0xFF)
1032+ data->vout_linear_11 = true;
1033+ }
1034+
1035+ return pmbus_do_probe(client, &data->info);
1036+}
1037+
1038+MODULE_DEVICE_TABLE(i2c, fsp3y_id);
1039+
1040+static struct i2c_driver fsp3y_driver = {
1041+ .driver = {
1042+ .name = "fsp3y",
1043+ },
1044+ .probe_new = fsp3y_probe,
1045+ .id_table = fsp3y_id
1046+};
1047+
1048+module_i2c_driver(fsp3y_driver);
1049+
1050+MODULE_AUTHOR("Václav Kubernát");
1051+MODULE_DESCRIPTION("PMBus driver for FSP/3Y-Power power supplies");
1052+MODULE_LICENSE("GPL");
Jan Kundrát25016432019-03-04 21:40:58 +01001053diff --git a/drivers/leds/leds-tlc591xx.c b/drivers/leds/leds-tlc591xx.c
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001054index 5b9dfdf743ec..26ea1164cb49 100644
Jan Kundrát25016432019-03-04 21:40:58 +01001055--- a/drivers/leds/leds-tlc591xx.c
1056+++ b/drivers/leds/leds-tlc591xx.c
Jan Kundrátf97c81d2020-02-06 00:33:19 +01001057@@ -40,6 +40,9 @@
Jan Kundrát25016432019-03-04 21:40:58 +01001058
1059 #define ldev_to_led(c) container_of(c, struct tlc591xx_led, ldev)
1060
1061+#define TLC591XX_RESET_BYTE_0 0xa5
1062+#define TLC591XX_RESET_BYTE_1 0x5a
1063+
1064 struct tlc591xx_led {
1065 bool active;
1066 unsigned int led_no;
Jan Kundrátf97c81d2020-02-06 00:33:19 +01001067@@ -51,21 +54,25 @@ struct tlc591xx_priv {
Jan Kundrát25016432019-03-04 21:40:58 +01001068 struct tlc591xx_led leds[TLC591XX_MAX_LEDS];
1069 struct regmap *regmap;
1070 unsigned int reg_ledout_offset;
1071+ struct i2c_client *swrst_client;
1072 };
1073
1074 struct tlc591xx {
1075 unsigned int max_leds;
1076 unsigned int reg_ledout_offset;
1077+ u8 swrst_addr;
1078 };
1079
1080 static const struct tlc591xx tlc59116 = {
1081 .max_leds = 16,
1082 .reg_ledout_offset = 0x14,
1083+ .swrst_addr = 0x6b,
1084 };
1085
1086 static const struct tlc591xx tlc59108 = {
1087 .max_leds = 8,
1088 .reg_ledout_offset = 0x0c,
1089+ .swrst_addr = 0x4b,
1090 };
1091
1092 static int
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001093@@ -176,6 +183,18 @@ tlc591xx_probe(struct i2c_client *client,
Jan Kundrát31bf7ae2020-02-14 17:03:53 +01001094
1095 i2c_set_clientdata(client, priv);
Jan Kundrát25016432019-03-04 21:40:58 +01001096
Jan Kundrátf97c81d2020-02-06 00:33:19 +01001097+ priv->swrst_client = devm_i2c_new_dummy_device(dev, client->adapter, tlc591xx->swrst_addr);
Jan Kundrát89b682a2020-03-08 13:03:33 -07001098+ if (IS_ERR(priv->swrst_client)) {
1099+ dev_info(dev, "Skipping reset: address %02x already used\n",
1100+ tlc591xx->swrst_addr);
1101+ } else {
Jan Kundrát25016432019-03-04 21:40:58 +01001102+ err = i2c_smbus_write_byte_data(priv->swrst_client,
1103+ TLC591XX_RESET_BYTE_0, TLC591XX_RESET_BYTE_1);
1104+ if (err) {
1105+ dev_warn(dev, "SW reset failed\n");
1106+ }
Jan Kundrát25016432019-03-04 21:40:58 +01001107+ }
1108+
Jan Kundrát31bf7ae2020-02-14 17:03:53 +01001109 err = tlc591xx_set_mode(priv->regmap, MODE2_DIM);
1110 if (err < 0)
1111 return err;
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001112diff --git a/drivers/net/ethernet/intel/igb/igb_main.c b/drivers/net/ethernet/intel/igb/igb_main.c
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001113index a45cd2b416c8..aa4c38a0952d 100644
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001114--- a/drivers/net/ethernet/intel/igb/igb_main.c
1115+++ b/drivers/net/ethernet/intel/igb/igb_main.c
1116@@ -577,16 +577,15 @@ static void igb_set_i2c_data(void *data, int state)
1117 struct e1000_hw *hw = &adapter->hw;
1118 s32 i2cctl = rd32(E1000_I2CPARAMS);
Jan Kundrát0341eea2020-10-16 18:06:38 +02001119
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001120- if (state)
1121- i2cctl |= E1000_I2C_DATA_OUT;
1122- else
1123+ if (state) {
1124+ i2cctl |= E1000_I2C_DATA_OUT | E1000_I2C_DATA_OE_N;
1125+ } else {
1126+ i2cctl &= ~E1000_I2C_DATA_OE_N;
1127 i2cctl &= ~E1000_I2C_DATA_OUT;
Jan Kundrát0341eea2020-10-16 18:06:38 +02001128+ }
1129
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001130- i2cctl &= ~E1000_I2C_DATA_OE_N;
1131- i2cctl |= E1000_I2C_CLK_OE_N;
1132 wr32(E1000_I2CPARAMS, i2cctl);
1133 wrfl();
1134-
Jan Kundrát0341eea2020-10-16 18:06:38 +02001135 }
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001136
1137 /**
1138@@ -603,8 +602,7 @@ static void igb_set_i2c_clk(void *data, int state)
1139 s32 i2cctl = rd32(E1000_I2CPARAMS);
1140
1141 if (state) {
1142- i2cctl |= E1000_I2C_CLK_OUT;
1143- i2cctl &= ~E1000_I2C_CLK_OE_N;
1144+ i2cctl |= E1000_I2C_CLK_OUT | E1000_I2C_CLK_OE_N;
1145 } else {
1146 i2cctl &= ~E1000_I2C_CLK_OUT;
1147 i2cctl &= ~E1000_I2C_CLK_OE_N;
1148@@ -3109,11 +3107,20 @@ static void igb_init_mas(struct igb_adapter *adapter)
1149 static s32 igb_init_i2c(struct igb_adapter *adapter)
1150 {
1151 s32 status = 0;
1152+ s32 i2cctl;
1153+ struct e1000_hw *hw = &adapter->hw;
1154
1155 /* I2C interface supported on i350 devices */
1156 if (adapter->hw.mac.type != e1000_i350)
1157 return 0;
1158
1159+ i2cctl = rd32(E1000_I2CPARAMS);
1160+ i2cctl |= E1000_I2CBB_EN
1161+ | E1000_I2C_CLK_OUT | E1000_I2C_CLK_OE_N
1162+ | E1000_I2C_DATA_OUT | E1000_I2C_DATA_OE_N;
1163+ wr32(E1000_I2CPARAMS, i2cctl);
1164+ wrfl();
Jan Kundrát0341eea2020-10-16 18:06:38 +02001165+
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001166 /* Initialize the i2c bus which is controlled by the registers.
1167 * This bus will use the i2c_algo_bit structue that implements
1168 * the protocol through toggling of the 4 bits in the register.
Jan Kundrát0341eea2020-10-16 18:06:38 +02001169diff --git a/drivers/pinctrl/pinctrl-mcp23s08_spi.c b/drivers/pinctrl/pinctrl-mcp23s08_spi.c
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001170index 9ae10318f6f3..d378369bbeca 100644
Jan Kundrát0341eea2020-10-16 18:06:38 +02001171--- a/drivers/pinctrl/pinctrl-mcp23s08_spi.c
1172+++ b/drivers/pinctrl/pinctrl-mcp23s08_spi.c
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001173@@ -8,6 +8,7 @@
1174 #include <linux/spi/spi.h>
Jan Kundrát0341eea2020-10-16 18:06:38 +02001175
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001176 #include "pinctrl-mcp23s08.h"
1177+#include "../gpio/gpiolib.h"
Jan Kundrát0341eea2020-10-16 18:06:38 +02001178
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001179 #define MCP_MAX_DEV_PER_CS 8
Jan Kundrát0341eea2020-10-16 18:06:38 +02001180
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001181@@ -143,6 +144,10 @@ static int mcp23s08_probe(struct spi_device *spi)
Jan Kundrát0341eea2020-10-16 18:06:38 +02001182 int type;
1183 int ret;
1184 u32 v;
1185+ struct device_node *np;
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001186+ int line_name_count;
1187+ const char **names;
1188+ int i;
Jan Kundrát0341eea2020-10-16 18:06:38 +02001189
1190 match = device_get_match_data(dev);
Jan Kundrát25016432019-03-04 21:40:58 +01001191 if (match)
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001192@@ -192,6 +197,43 @@ static int mcp23s08_probe(struct spi_device *spi)
Jan Kundrát0341eea2020-10-16 18:06:38 +02001193 return ret;
1194
Jan Kundrát25016432019-03-04 21:40:58 +01001195 ngpio += data->mcp[addr]->chip.ngpio;
1196+
1197+ for_each_available_child_of_node(spi->dev.of_node, np) {
1198+ u32 chip_addr;
Jan Kundrát0341eea2020-10-16 18:06:38 +02001199+ ret = of_property_read_u32(np, "address", &chip_addr);
1200+ if (ret)
Jan Kundrát25016432019-03-04 21:40:58 +01001201+ continue;
1202+ if (chip_addr != addr)
1203+ continue;
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001204+
1205+ line_name_count = fwnode_property_read_string_array(of_fwnode_handle(np), "gpio-line-names", NULL, 0);
1206+ if (line_name_count < 0)
1207+ continue;
1208+
1209+ if (line_name_count > data->mcp[addr]->chip.ngpio) {
1210+ dev_warn(&spi->dev, "gpio-line-names is length %d but should be at most length %d",
1211+ line_name_count, data->mcp[addr]->chip.ngpio);
1212+ line_name_count = data->mcp[addr]->chip.ngpio;
1213+ }
1214+
1215+ names = kcalloc(line_name_count, sizeof(*names), GFP_KERNEL);
1216+ if (!names) {
1217+ dev_warn(&spi->dev, "cannot allocate gpio-line-names");
1218+ continue;
1219+ }
1220+
1221+ ret = fwnode_property_read_string_array(of_fwnode_handle(np), "gpio-line-names", names, line_name_count);
1222+ if (ret < 0) {
1223+ dev_warn(&spi->dev, "failed to read GPIO line names");
1224+ kfree(names);
1225+ continue;
1226+ }
1227+
1228+ for (i = 0; i < line_name_count; i++)
1229+ data->mcp[addr]->chip.gpiodev->descs[i].name = names[i];
1230+
1231+ kfree(names);
Jan Kundrát25016432019-03-04 21:40:58 +01001232+ }
1233 }
1234 data->ngpio = ngpio;
1235
Jan Kundráte7d21622019-03-28 22:52:38 +01001236diff --git a/drivers/spi/spi-orion.c b/drivers/spi/spi-orion.c
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001237index 68ed7fd64256..3fd04d5eeb14 100644
Jan Kundráte7d21622019-03-28 22:52:38 +01001238--- a/drivers/spi/spi-orion.c
1239+++ b/drivers/spi/spi-orion.c
Jan Kundrátbf1e0912021-03-05 12:45:23 +01001240@@ -86,10 +86,6 @@ struct orion_direct_acc {
Jan Kundráte7d21622019-03-28 22:52:38 +01001241 u32 size;
1242 };
1243
1244-struct orion_child_options {
1245- struct orion_direct_acc direct_access;
1246-};
1247-
Jan Kundrát25016432019-03-04 21:40:58 +01001248 struct orion_spi {
Jan Kundráte7d21622019-03-28 22:52:38 +01001249 struct spi_master *master;
1250 void __iomem *base;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001251@@ -98,7 +94,7 @@ struct orion_spi {
Jan Kundráte7d21622019-03-28 22:52:38 +01001252 const struct orion_spi_dev *devdata;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001253 struct device *dev;
Jan Kundráte7d21622019-03-28 22:52:38 +01001254
1255- struct orion_child_options child[ORION_NUM_CHIPSELECTS];
1256+ struct orion_direct_acc direct_access[ORION_NUM_CHIPSELECTS];
1257 };
1258
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001259 #ifdef CONFIG_PM
1260@@ -463,7 +459,7 @@ orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
Jan Kundrát25016432019-03-04 21:40:58 +01001261 int cs = spi->chip_select;
Jan Kundrát549db872019-03-05 12:23:25 +01001262 void __iomem *vaddr;
Jan Kundrát25016432019-03-04 21:40:58 +01001263
1264- word_len = spi->bits_per_word;
1265+ word_len = xfer->bits_per_word;
1266 count = xfer->len;
1267
1268 orion_spi = spi_master_get_devdata(spi->master);
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001269@@ -473,7 +469,7 @@ orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
1270 * and SPI_CS_WORD flag is not set.
1271 * Otherwise fall back to PIO mode for this transfer.
Jan Kundráte7d21622019-03-28 22:52:38 +01001272 */
1273- vaddr = orion_spi->child[cs].direct_access.vaddr;
1274+ vaddr = orion_spi->direct_access[cs].vaddr;
1275
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001276 if (vaddr && xfer->tx_buf && word_len == 8 && (spi->mode & SPI_CS_WORD) == 0) {
Jan Kundráte7d21622019-03-28 22:52:38 +01001277 unsigned int cnt = count / 4;
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001278@@ -727,7 +723,6 @@ static int orion_spi_probe(struct platform_device *pdev)
Jan Kundráte7d21622019-03-28 22:52:38 +01001279 }
1280
1281 for_each_available_child_of_node(pdev->dev.of_node, np) {
1282- struct orion_direct_acc *dir_acc;
1283 u32 cs;
Jan Kundráte7d21622019-03-28 22:52:38 +01001284
Jan Kundrát0341eea2020-10-16 18:06:38 +02001285 /* Get chip-select number from the "reg" property */
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001286@@ -756,13 +751,14 @@ static int orion_spi_probe(struct platform_device *pdev)
Jan Kundrát0341eea2020-10-16 18:06:38 +02001287 * This needs to get extended for the direct SPI NOR / SPI NAND
Jan Kundráte7d21622019-03-28 22:52:38 +01001288 * support, once this gets implemented.
1289 */
1290- dir_acc = &spi->child[cs].direct_access;
1291- dir_acc->vaddr = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
1292- if (!dir_acc->vaddr) {
1293+ spi->direct_access[cs].vaddr = devm_ioremap(&pdev->dev,
1294+ r->start,
1295+ PAGE_SIZE);
1296+ if (!spi->direct_access[cs].vaddr) {
1297 status = -ENOMEM;
1298 goto out_rel_axi_clk;
1299 }
1300- dir_acc->size = PAGE_SIZE;
1301+ spi->direct_access[cs].size = PAGE_SIZE;
1302
1303 dev_info(&pdev->dev, "CS%d configured for direct access\n", cs);
1304 }
Jan Kundráte7d21622019-03-28 22:52:38 +01001305diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001306index 1b61d26bb7af..e92f3c02c7f4 100644
Jan Kundráte7d21622019-03-28 22:52:38 +01001307--- a/drivers/tty/serial/max310x.c
1308+++ b/drivers/tty/serial/max310x.c
Jan Kundrát2bfe9ee2019-12-04 13:42:07 +01001309@@ -235,6 +235,10 @@
1310 #define MAX310x_REV_MASK (0xf8)
1311 #define MAX310X_WRITE_BIT 0x80
Jan Kundráte7d21622019-03-28 22:52:38 +01001312
Jan Kundrát2bfe9ee2019-12-04 13:42:07 +01001313+/* Timeout for external crystal stability */
1314+#define MAX310X_XTAL_WAIT_RETRIES 20
1315+#define MAX310X_XTAL_WAIT_DELAY_MS 10
1316+
1317 /* MAX3107 specific */
1318 #define MAX3107_REV_ID (0xa0)
Jan Kundráte7d21622019-03-28 22:52:38 +01001319
Jan Kundrát2bfe9ee2019-12-04 13:42:07 +01001320@@ -610,11 +614,14 @@ static int max310x_set_ref_clk(struct device *dev, struct max310x_port *s,
1321
1322 /* Wait for crystal */
1323 if (xtal) {
1324- unsigned int val;
1325- msleep(10);
1326- regmap_read(s->regmap, MAX310X_STS_IRQSTS_REG, &val);
1327+ unsigned int val = 0, i;
1328+ for (i = 0; i < MAX310X_XTAL_WAIT_RETRIES && !(val & MAX310X_STS_CLKREADY_BIT); ++i) {
1329+ msleep(MAX310X_XTAL_WAIT_DELAY_MS);
1330+ regmap_read(s->regmap, MAX310X_STS_IRQSTS_REG, &val);
1331+ }
1332 if (!(val & MAX310X_STS_CLKREADY_BIT)) {
1333- dev_warn(dev, "clock is not stable yet\n");
1334+ dev_err(dev, "clock is not stable\n");
1335+ return -EAGAIN;
1336 }
1337 }
1338
Václav Kubernátf1b92ff2021-03-24 07:41:23 +01001339@@ -1327,6 +1334,10 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
Jan Kundráte7d21622019-03-28 22:52:38 +01001340 }
1341
1342 uartclk = max310x_set_ref_clk(dev, s, freq, xtal);
Jan Kundrát2bfe9ee2019-12-04 13:42:07 +01001343+ if (uartclk < 0) {
1344+ ret = uartclk;
1345+ goto out_uart;
1346+ }
1347 dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
Jan Kundráte7d21622019-03-28 22:52:38 +01001348
Jan Kundrát2bfe9ee2019-12-04 13:42:07 +01001349 for (i = 0; i < devtype->nr; i++) {