1 // SPDX-License-Identifier: GPL-2.0
3 * TI TPS68470 PMIC operation region driver
5 * Copyright (C) 2017 Intel Corporation. All rights reserved.
7 * Author: Rajmohan Mani <rajmohan.mani@intel.com>
9 * Based on drivers/acpi/pmic/intel_pmic* drivers
12 #include <linux/acpi.h>
13 #include <linux/init.h>
14 #include <linux/mfd/tps68470.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
18 struct tps68470_pmic_table
{
19 u32 address
; /* operation region address */
20 u32 reg
; /* corresponding register */
21 u32 bitmask
; /* bit mask for power, clock */
24 #define TI_PMIC_POWER_OPREGION_ID 0xB0
25 #define TI_PMIC_VR_VAL_OPREGION_ID 0xB1
26 #define TI_PMIC_CLOCK_OPREGION_ID 0xB2
27 #define TI_PMIC_CLKFREQ_OPREGION_ID 0xB3
29 struct tps68470_pmic_opregion
{
31 struct regmap
*regmap
;
34 #define S_IO_I2C_EN (BIT(0) | BIT(1))
36 static const struct tps68470_pmic_table power_table
[] = {
39 .reg
= TPS68470_REG_S_I2C_CTL
,
40 .bitmask
= S_IO_I2C_EN
,
45 .reg
= TPS68470_REG_VCMCTL
,
51 .reg
= TPS68470_REG_VAUX1CTL
,
57 .reg
= TPS68470_REG_VAUX2CTL
,
63 .reg
= TPS68470_REG_VACTL
,
69 .reg
= TPS68470_REG_VDCTL
,
75 /* Table to set voltage regulator value */
76 static const struct tps68470_pmic_table vr_val_table
[] = {
79 .reg
= TPS68470_REG_VSIOVAL
,
80 .bitmask
= TPS68470_VSIOVAL_IOVOLT_MASK
,
81 /* TPS68470_REG_VSIOVAL */
85 .reg
= TPS68470_REG_VIOVAL
,
86 .bitmask
= TPS68470_VIOVAL_IOVOLT_MASK
,
87 /* TPS68470_REG_VIOVAL */
91 .reg
= TPS68470_REG_VCMVAL
,
92 .bitmask
= TPS68470_VCMVAL_VCVOLT_MASK
,
93 /* TPS68470_REG_VCMVAL */
97 .reg
= TPS68470_REG_VAUX1VAL
,
98 .bitmask
= TPS68470_VAUX1VAL_AUX1VOLT_MASK
,
99 /* TPS68470_REG_VAUX1VAL */
103 .reg
= TPS68470_REG_VAUX2VAL
,
104 .bitmask
= TPS68470_VAUX2VAL_AUX2VOLT_MASK
,
105 /* TPS68470_REG_VAUX2VAL */
109 .reg
= TPS68470_REG_VAVAL
,
110 .bitmask
= TPS68470_VAVAL_AVOLT_MASK
,
111 /* TPS68470_REG_VAVAL */
115 .reg
= TPS68470_REG_VDVAL
,
116 .bitmask
= TPS68470_VDVAL_DVOLT_MASK
,
117 /* TPS68470_REG_VDVAL */
121 /* Table to configure clock frequency */
122 static const struct tps68470_pmic_table clk_freq_table
[] = {
125 .reg
= TPS68470_REG_POSTDIV2
,
126 .bitmask
= BIT(0) | BIT(1),
127 /* TPS68470_REG_POSTDIV2 */
131 .reg
= TPS68470_REG_BOOSTDIV
,
133 /* TPS68470_REG_BOOSTDIV */
137 .reg
= TPS68470_REG_BUCKDIV
,
139 /* TPS68470_REG_BUCKDIV */
143 .reg
= TPS68470_REG_PLLSWR
,
145 /* TPS68470_REG_PLLSWR */
149 .reg
= TPS68470_REG_XTALDIV
,
151 /* TPS68470_REG_XTALDIV */
155 .reg
= TPS68470_REG_PLLDIV
,
157 /* TPS68470_REG_PLLDIV */
161 .reg
= TPS68470_REG_POSTDIV
,
163 /* TPS68470_REG_POSTDIV */
167 /* Table to configure and enable clocks */
168 static const struct tps68470_pmic_table clk_table
[] = {
171 .reg
= TPS68470_REG_PLLCTL
,
173 /* TPS68470_REG_PLLCTL */
177 .reg
= TPS68470_REG_PLLCTL2
,
179 /* TPS68470_REG_PLLCTL2 */
183 .reg
= TPS68470_REG_CLKCFG1
,
184 .bitmask
= TPS68470_CLKCFG1_MODE_A_MASK
|
185 TPS68470_CLKCFG1_MODE_B_MASK
,
186 /* TPS68470_REG_CLKCFG1 */
190 .reg
= TPS68470_REG_CLKCFG2
,
191 .bitmask
= TPS68470_CLKCFG1_MODE_A_MASK
|
192 TPS68470_CLKCFG1_MODE_B_MASK
,
193 /* TPS68470_REG_CLKCFG2 */
197 static int pmic_get_reg_bit(u64 address
,
198 const struct tps68470_pmic_table
*table
,
199 const unsigned int table_size
, int *reg
,
208 if (!reg
|| !bitmask
)
212 *bitmask
= table
[i
].bitmask
;
217 static int tps68470_pmic_get_power(struct regmap
*regmap
, int reg
,
218 int bitmask
, u64
*value
)
222 if (regmap_read(regmap
, reg
, &data
))
225 *value
= (data
& bitmask
) ? 1 : 0;
229 static int tps68470_pmic_get_vr_val(struct regmap
*regmap
, int reg
,
230 int bitmask
, u64
*value
)
234 if (regmap_read(regmap
, reg
, &data
))
237 *value
= data
& bitmask
;
241 static int tps68470_pmic_get_clk(struct regmap
*regmap
, int reg
,
242 int bitmask
, u64
*value
)
246 if (regmap_read(regmap
, reg
, &data
))
249 *value
= (data
& bitmask
) ? 1 : 0;
253 static int tps68470_pmic_get_clk_freq(struct regmap
*regmap
, int reg
,
254 int bitmask
, u64
*value
)
258 if (regmap_read(regmap
, reg
, &data
))
261 *value
= data
& bitmask
;
265 static int ti_tps68470_regmap_update_bits(struct regmap
*regmap
, int reg
,
266 int bitmask
, u64 value
)
268 return regmap_update_bits(regmap
, reg
, bitmask
, value
);
271 static acpi_status
tps68470_pmic_common_handler(u32 function
,
272 acpi_physical_address address
,
273 u32 bits
, u64
*value
,
274 void *region_context
,
275 int (*get
)(struct regmap
*,
277 int (*update
)(struct regmap
*,
279 const struct tps68470_pmic_table
*tbl
,
280 unsigned int tbl_size
)
282 struct tps68470_pmic_opregion
*opregion
= region_context
;
283 struct regmap
*regmap
= opregion
->regmap
;
284 int reg
, ret
, bitmask
;
287 return AE_BAD_PARAMETER
;
289 ret
= pmic_get_reg_bit(address
, tbl
, tbl_size
, ®
, &bitmask
);
291 return AE_BAD_PARAMETER
;
293 if (function
== ACPI_WRITE
&& *value
> bitmask
)
294 return AE_BAD_PARAMETER
;
296 mutex_lock(&opregion
->lock
);
298 ret
= (function
== ACPI_READ
) ?
299 get(regmap
, reg
, bitmask
, value
) :
300 update(regmap
, reg
, bitmask
, *value
);
302 mutex_unlock(&opregion
->lock
);
304 return ret
? AE_ERROR
: AE_OK
;
307 static acpi_status
tps68470_pmic_cfreq_handler(u32 function
,
308 acpi_physical_address address
,
309 u32 bits
, u64
*value
,
310 void *handler_context
,
311 void *region_context
)
313 return tps68470_pmic_common_handler(function
, address
, bits
, value
,
315 tps68470_pmic_get_clk_freq
,
316 ti_tps68470_regmap_update_bits
,
318 ARRAY_SIZE(clk_freq_table
));
321 static acpi_status
tps68470_pmic_clk_handler(u32 function
,
322 acpi_physical_address address
, u32 bits
,
323 u64
*value
, void *handler_context
,
324 void *region_context
)
326 return tps68470_pmic_common_handler(function
, address
, bits
, value
,
328 tps68470_pmic_get_clk
,
329 ti_tps68470_regmap_update_bits
,
331 ARRAY_SIZE(clk_table
));
334 static acpi_status
tps68470_pmic_vrval_handler(u32 function
,
335 acpi_physical_address address
,
336 u32 bits
, u64
*value
,
337 void *handler_context
,
338 void *region_context
)
340 return tps68470_pmic_common_handler(function
, address
, bits
, value
,
342 tps68470_pmic_get_vr_val
,
343 ti_tps68470_regmap_update_bits
,
345 ARRAY_SIZE(vr_val_table
));
348 static acpi_status
tps68470_pmic_pwr_handler(u32 function
,
349 acpi_physical_address address
,
350 u32 bits
, u64
*value
,
351 void *handler_context
,
352 void *region_context
)
355 return AE_BAD_PARAMETER
;
357 /* set/clear for bit 0, bits 0 and 1 together */
358 if (function
== ACPI_WRITE
&&
359 !(*value
== 0 || *value
== 1 || *value
== 3)) {
360 return AE_BAD_PARAMETER
;
363 return tps68470_pmic_common_handler(function
, address
, bits
, value
,
365 tps68470_pmic_get_power
,
366 ti_tps68470_regmap_update_bits
,
368 ARRAY_SIZE(power_table
));
371 static int tps68470_pmic_opregion_probe(struct platform_device
*pdev
)
373 struct regmap
*tps68470_regmap
= dev_get_drvdata(pdev
->dev
.parent
);
374 acpi_handle handle
= ACPI_HANDLE(pdev
->dev
.parent
);
375 struct device
*dev
= &pdev
->dev
;
376 struct tps68470_pmic_opregion
*opregion
;
379 if (!tps68470_regmap
)
380 return dev_err_probe(dev
, -EINVAL
, "regmap is missing\n");
383 dev_warn(dev
, "acpi handle is NULL\n");
387 opregion
= devm_kzalloc(dev
, sizeof(*opregion
), GFP_KERNEL
);
391 mutex_init(&opregion
->lock
);
392 opregion
->regmap
= tps68470_regmap
;
394 status
= acpi_install_address_space_handler(handle
,
395 TI_PMIC_POWER_OPREGION_ID
,
396 tps68470_pmic_pwr_handler
,
398 if (ACPI_FAILURE(status
))
399 goto out_mutex_destroy
;
401 status
= acpi_install_address_space_handler(handle
,
402 TI_PMIC_VR_VAL_OPREGION_ID
,
403 tps68470_pmic_vrval_handler
,
405 if (ACPI_FAILURE(status
))
406 goto out_remove_power_handler
;
408 status
= acpi_install_address_space_handler(handle
,
409 TI_PMIC_CLOCK_OPREGION_ID
,
410 tps68470_pmic_clk_handler
,
412 if (ACPI_FAILURE(status
))
413 goto out_remove_vr_val_handler
;
415 status
= acpi_install_address_space_handler(handle
,
416 TI_PMIC_CLKFREQ_OPREGION_ID
,
417 tps68470_pmic_cfreq_handler
,
419 if (ACPI_FAILURE(status
))
420 goto out_remove_clk_handler
;
424 out_remove_clk_handler
:
425 acpi_remove_address_space_handler(handle
, TI_PMIC_CLOCK_OPREGION_ID
,
426 tps68470_pmic_clk_handler
);
427 out_remove_vr_val_handler
:
428 acpi_remove_address_space_handler(handle
, TI_PMIC_VR_VAL_OPREGION_ID
,
429 tps68470_pmic_vrval_handler
);
430 out_remove_power_handler
:
431 acpi_remove_address_space_handler(handle
, TI_PMIC_POWER_OPREGION_ID
,
432 tps68470_pmic_pwr_handler
);
434 mutex_destroy(&opregion
->lock
);
438 static struct platform_driver tps68470_pmic_opregion_driver
= {
439 .probe
= tps68470_pmic_opregion_probe
,
441 .name
= "tps68470_pmic_opregion",
445 builtin_platform_driver(tps68470_pmic_opregion_driver
)