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/mfd/tps68470.h>
14 #include <linux/init.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 (!dev
|| !tps68470_regmap
) {
380 dev_warn(dev
, "dev or regmap is NULL\n");
385 dev_warn(dev
, "acpi handle is NULL\n");
389 opregion
= devm_kzalloc(dev
, sizeof(*opregion
), GFP_KERNEL
);
393 mutex_init(&opregion
->lock
);
394 opregion
->regmap
= tps68470_regmap
;
396 status
= acpi_install_address_space_handler(handle
,
397 TI_PMIC_POWER_OPREGION_ID
,
398 tps68470_pmic_pwr_handler
,
400 if (ACPI_FAILURE(status
))
401 goto out_mutex_destroy
;
403 status
= acpi_install_address_space_handler(handle
,
404 TI_PMIC_VR_VAL_OPREGION_ID
,
405 tps68470_pmic_vrval_handler
,
407 if (ACPI_FAILURE(status
))
408 goto out_remove_power_handler
;
410 status
= acpi_install_address_space_handler(handle
,
411 TI_PMIC_CLOCK_OPREGION_ID
,
412 tps68470_pmic_clk_handler
,
414 if (ACPI_FAILURE(status
))
415 goto out_remove_vr_val_handler
;
417 status
= acpi_install_address_space_handler(handle
,
418 TI_PMIC_CLKFREQ_OPREGION_ID
,
419 tps68470_pmic_cfreq_handler
,
421 if (ACPI_FAILURE(status
))
422 goto out_remove_clk_handler
;
426 out_remove_clk_handler
:
427 acpi_remove_address_space_handler(handle
, TI_PMIC_CLOCK_OPREGION_ID
,
428 tps68470_pmic_clk_handler
);
429 out_remove_vr_val_handler
:
430 acpi_remove_address_space_handler(handle
, TI_PMIC_VR_VAL_OPREGION_ID
,
431 tps68470_pmic_vrval_handler
);
432 out_remove_power_handler
:
433 acpi_remove_address_space_handler(handle
, TI_PMIC_POWER_OPREGION_ID
,
434 tps68470_pmic_pwr_handler
);
436 mutex_destroy(&opregion
->lock
);
440 static struct platform_driver tps68470_pmic_opregion_driver
= {
441 .probe
= tps68470_pmic_opregion_probe
,
443 .name
= "tps68470_pmic_opregion",
447 builtin_platform_driver(tps68470_pmic_opregion_driver
)