2018-08-07 19:18:25 +03:00
// SPDX-License-Identifier: GPL-2.0+
//
// max8998.c - Voltage regulator driver for the Maxim 8998
//
// Copyright (C) 2009-2010 Samsung Electronics
// Kyungmin Park <kyungmin.park@samsung.com>
// Marek Szyprowski <m.szyprowski@samsung.com>
2010-06-16 11:04:16 +04:00
# include <linux/module.h>
# include <linux/init.h>
# include <linux/i2c.h>
# include <linux/err.h>
# include <linux/gpio.h>
# include <linux/slab.h>
# include <linux/interrupt.h>
# include <linux/mutex.h>
2013-06-25 18:08:10 +04:00
# include <linux/of.h>
# include <linux/of_gpio.h>
2010-06-16 11:04:16 +04:00
# include <linux/platform_device.h>
# include <linux/regulator/driver.h>
2013-06-25 18:08:10 +04:00
# include <linux/regulator/of_regulator.h>
2010-06-16 11:04:16 +04:00
# include <linux/mfd/max8998.h>
# include <linux/mfd/max8998-private.h>
struct max8998_data {
struct device * dev ;
struct max8998_dev * iodev ;
int num_regulators ;
2010-09-27 16:32:25 +04:00
u8 buck1_vol [ 4 ] ; /* voltages for selection */
u8 buck2_vol [ 2 ] ;
unsigned int buck1_idx ; /* index to last changed voltage */
/* value in a set */
unsigned int buck2_idx ;
2010-06-16 11:04:16 +04:00
} ;
static int max8998_get_enable_register ( struct regulator_dev * rdev ,
int * reg , int * shift )
{
2012-03-01 05:27:29 +04:00
int ldo = rdev_get_id ( rdev ) ;
2010-06-16 11:04:16 +04:00
switch ( ldo ) {
case MAX8998_LDO2 . . . MAX8998_LDO5 :
* reg = MAX8998_REG_ONOFF1 ;
* shift = 3 - ( ldo - MAX8998_LDO2 ) ;
break ;
case MAX8998_LDO6 . . . MAX8998_LDO13 :
* reg = MAX8998_REG_ONOFF2 ;
* shift = 7 - ( ldo - MAX8998_LDO6 ) ;
break ;
case MAX8998_LDO14 . . . MAX8998_LDO17 :
* reg = MAX8998_REG_ONOFF3 ;
* shift = 7 - ( ldo - MAX8998_LDO14 ) ;
break ;
case MAX8998_BUCK1 . . . MAX8998_BUCK4 :
* reg = MAX8998_REG_ONOFF1 ;
* shift = 7 - ( ldo - MAX8998_BUCK1 ) ;
break ;
case MAX8998_EN32KHZ_AP . . . MAX8998_ENVICHG :
* reg = MAX8998_REG_ONOFF4 ;
* shift = 7 - ( ldo - MAX8998_EN32KHZ_AP ) ;
break ;
case MAX8998_ESAFEOUT1 . . . MAX8998_ESAFEOUT2 :
* reg = MAX8998_REG_CHGR2 ;
* shift = 7 - ( ldo - MAX8998_ESAFEOUT1 ) ;
break ;
default :
return - EINVAL ;
}
return 0 ;
}
static int max8998_ldo_is_enabled ( struct regulator_dev * rdev )
{
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
2010-08-06 06:28:06 +04:00
struct i2c_client * i2c = max8998 - > iodev - > i2c ;
2010-06-16 11:04:16 +04:00
int ret , reg , shift = 8 ;
u8 val ;
ret = max8998_get_enable_register ( rdev , & reg , & shift ) ;
if ( ret )
return ret ;
2010-08-06 06:28:06 +04:00
ret = max8998_read_reg ( i2c , reg , & val ) ;
2010-06-16 11:04:16 +04:00
if ( ret )
return ret ;
return val & ( 1 < < shift ) ;
}
static int max8998_ldo_enable ( struct regulator_dev * rdev )
{
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
2010-08-06 06:28:06 +04:00
struct i2c_client * i2c = max8998 - > iodev - > i2c ;
2010-06-16 11:04:16 +04:00
int reg , shift = 8 , ret ;
ret = max8998_get_enable_register ( rdev , & reg , & shift ) ;
if ( ret )
return ret ;
2010-08-06 06:28:06 +04:00
return max8998_update_reg ( i2c , reg , 1 < < shift , 1 < < shift ) ;
2010-06-16 11:04:16 +04:00
}
static int max8998_ldo_disable ( struct regulator_dev * rdev )
{
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
2010-08-06 06:28:06 +04:00
struct i2c_client * i2c = max8998 - > iodev - > i2c ;
2010-06-16 11:04:16 +04:00
int reg , shift = 8 , ret ;
ret = max8998_get_enable_register ( rdev , & reg , & shift ) ;
if ( ret )
return ret ;
2010-08-06 06:28:06 +04:00
return max8998_update_reg ( i2c , reg , 0 , 1 < < shift ) ;
2010-06-16 11:04:16 +04:00
}
static int max8998_get_voltage_register ( struct regulator_dev * rdev ,
int * _reg , int * _shift , int * _mask )
{
2012-03-01 05:27:29 +04:00
int ldo = rdev_get_id ( rdev ) ;
2010-09-27 16:32:25 +04:00
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
2010-06-16 11:04:16 +04:00
int reg , shift = 0 , mask = 0xff ;
switch ( ldo ) {
case MAX8998_LDO2 . . . MAX8998_LDO3 :
reg = MAX8998_REG_LDO2_LDO3 ;
mask = 0xf ;
if ( ldo = = MAX8998_LDO2 )
shift = 4 ;
else
shift = 0 ;
break ;
case MAX8998_LDO4 . . . MAX8998_LDO7 :
reg = MAX8998_REG_LDO4 + ( ldo - MAX8998_LDO4 ) ;
break ;
case MAX8998_LDO8 . . . MAX8998_LDO9 :
reg = MAX8998_REG_LDO8_LDO9 ;
mask = 0xf ;
if ( ldo = = MAX8998_LDO8 )
shift = 4 ;
else
shift = 0 ;
break ;
case MAX8998_LDO10 . . . MAX8998_LDO11 :
reg = MAX8998_REG_LDO10_LDO11 ;
if ( ldo = = MAX8998_LDO10 ) {
shift = 5 ;
mask = 0x7 ;
} else {
shift = 0 ;
mask = 0x1f ;
}
break ;
case MAX8998_LDO12 . . . MAX8998_LDO17 :
reg = MAX8998_REG_LDO12 + ( ldo - MAX8998_LDO12 ) ;
break ;
case MAX8998_BUCK1 :
2010-09-27 16:32:25 +04:00
reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998 - > buck1_idx ;
2010-06-16 11:04:16 +04:00
break ;
case MAX8998_BUCK2 :
2010-09-27 16:32:25 +04:00
reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998 - > buck2_idx ;
2010-06-16 11:04:16 +04:00
break ;
case MAX8998_BUCK3 :
reg = MAX8998_REG_BUCK3 ;
break ;
case MAX8998_BUCK4 :
reg = MAX8998_REG_BUCK4 ;
break ;
default :
return - EINVAL ;
}
* _reg = reg ;
* _shift = shift ;
* _mask = mask ;
return 0 ;
}
2012-04-11 09:58:09 +04:00
static int max8998_get_voltage_sel ( struct regulator_dev * rdev )
2010-06-16 11:04:16 +04:00
{
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
2010-08-06 06:28:06 +04:00
struct i2c_client * i2c = max8998 - > iodev - > i2c ;
2010-06-16 11:04:16 +04:00
int reg , shift = 0 , mask , ret ;
u8 val ;
ret = max8998_get_voltage_register ( rdev , & reg , & shift , & mask ) ;
if ( ret )
return ret ;
2010-08-06 06:28:06 +04:00
ret = max8998_read_reg ( i2c , reg , & val ) ;
2010-06-16 11:04:16 +04:00
if ( ret )
return ret ;
val > > = shift ;
val & = mask ;
2012-04-11 09:58:09 +04:00
return val ;
2010-06-16 11:04:16 +04:00
}
2012-06-17 05:34:29 +04:00
static int max8998_set_voltage_ldo_sel ( struct regulator_dev * rdev ,
unsigned selector )
2010-09-27 16:32:23 +04:00
{
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
struct i2c_client * i2c = max8998 - > iodev - > i2c ;
2012-06-17 05:34:29 +04:00
int reg , shift = 0 , mask , ret ;
2010-11-10 17:38:29 +03:00
2010-09-27 16:32:23 +04:00
ret = max8998_get_voltage_register ( rdev , & reg , & shift , & mask ) ;
if ( ret )
return ret ;
2012-06-17 05:34:29 +04:00
ret = max8998_update_reg ( i2c , reg , selector < < shift , mask < < shift ) ;
2010-09-27 16:32:23 +04:00
return ret ;
}
2010-09-27 16:32:27 +04:00
static inline void buck1_gpio_set ( int gpio1 , int gpio2 , int v )
{
gpio_set_value ( gpio1 , v & 0x1 ) ;
gpio_set_value ( gpio2 , ( v > > 1 ) & 0x1 ) ;
}
static inline void buck2_gpio_set ( int gpio , int v )
{
gpio_set_value ( gpio , v & 0x1 ) ;
}
2012-06-17 05:34:29 +04:00
static int max8998_set_voltage_buck_sel ( struct regulator_dev * rdev ,
unsigned selector )
2010-06-16 11:04:16 +04:00
{
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
2018-04-27 19:02:59 +03:00
struct max8998_platform_data * pdata = max8998 - > iodev - > pdata ;
2010-08-06 06:28:06 +04:00
struct i2c_client * i2c = max8998 - > iodev - > i2c ;
2012-03-01 05:27:29 +04:00
int buck = rdev_get_id ( rdev ) ;
2013-02-06 07:10:51 +04:00
int reg , shift = 0 , mask , ret , j ;
2010-09-27 16:32:27 +04:00
static u8 buck1_last_val ;
2010-06-16 11:04:16 +04:00
ret = max8998_get_voltage_register ( rdev , & reg , & shift , & mask ) ;
if ( ret )
return ret ;
2010-09-27 16:32:27 +04:00
switch ( buck ) {
case MAX8998_BUCK1 :
dev_dbg ( max8998 - > dev ,
2012-06-17 05:34:29 +04:00
" BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d \n "
2011-04-24 07:38:19 +04:00
" buck1_vol3:%d, buck1_vol4:%d \n " ,
2012-06-17 05:34:29 +04:00
selector , max8998 - > buck1_vol [ 0 ] , max8998 - > buck1_vol [ 1 ] ,
2010-09-27 16:32:27 +04:00
max8998 - > buck1_vol [ 2 ] , max8998 - > buck1_vol [ 3 ] ) ;
if ( gpio_is_valid ( pdata - > buck1_set1 ) & &
gpio_is_valid ( pdata - > buck1_set2 ) ) {
/* check if requested voltage */
/* value is already defined */
for ( j = 0 ; j < ARRAY_SIZE ( max8998 - > buck1_vol ) ; j + + ) {
2012-06-17 05:34:29 +04:00
if ( max8998 - > buck1_vol [ j ] = = selector ) {
2010-09-27 16:32:27 +04:00
max8998 - > buck1_idx = j ;
buck1_gpio_set ( pdata - > buck1_set1 ,
pdata - > buck1_set2 , j ) ;
goto buck1_exit ;
}
}
2011-01-11 14:20:05 +03:00
if ( pdata - > buck_voltage_lock )
return - EINVAL ;
2010-09-27 16:32:27 +04:00
/* no predefine regulator found */
max8998 - > buck1_idx = ( buck1_last_val % 2 ) + 2 ;
dev_dbg ( max8998 - > dev , " max8998->buck1_idx:%d \n " ,
max8998 - > buck1_idx ) ;
2012-06-17 05:34:29 +04:00
max8998 - > buck1_vol [ max8998 - > buck1_idx ] = selector ;
2010-09-27 16:32:27 +04:00
ret = max8998_get_voltage_register ( rdev , & reg ,
& shift ,
& mask ) ;
2012-06-17 05:34:29 +04:00
ret = max8998_write_reg ( i2c , reg , selector ) ;
2010-09-27 16:32:27 +04:00
buck1_gpio_set ( pdata - > buck1_set1 ,
pdata - > buck1_set2 , max8998 - > buck1_idx ) ;
buck1_last_val + + ;
buck1_exit :
dev_dbg ( max8998 - > dev , " %s: SET1:%d, SET2:%d \n " ,
i2c - > name , gpio_get_value ( pdata - > buck1_set1 ) ,
gpio_get_value ( pdata - > buck1_set2 ) ) ;
break ;
} else {
2012-06-17 05:34:29 +04:00
ret = max8998_write_reg ( i2c , reg , selector ) ;
2010-09-27 16:32:27 +04:00
}
break ;
2010-07-20 10:07:07 +04:00
2010-09-27 16:32:27 +04:00
case MAX8998_BUCK2 :
dev_dbg ( max8998 - > dev ,
2012-06-17 05:34:29 +04:00
" BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d \n " ,
selector , max8998 - > buck2_vol [ 0 ] , max8998 - > buck2_vol [ 1 ] ) ;
2010-09-27 16:32:27 +04:00
if ( gpio_is_valid ( pdata - > buck2_set3 ) ) {
2011-01-11 14:20:05 +03:00
/* check if requested voltage */
/* value is already defined */
for ( j = 0 ; j < ARRAY_SIZE ( max8998 - > buck2_vol ) ; j + + ) {
2012-06-17 05:34:29 +04:00
if ( max8998 - > buck2_vol [ j ] = = selector ) {
2011-01-11 14:20:05 +03:00
max8998 - > buck2_idx = j ;
buck2_gpio_set ( pdata - > buck2_set3 , j ) ;
goto buck2_exit ;
}
2010-09-27 16:32:27 +04:00
}
2011-01-11 14:20:05 +03:00
if ( pdata - > buck_voltage_lock )
return - EINVAL ;
max8998_get_voltage_register ( rdev ,
& reg , & shift , & mask ) ;
2012-06-17 05:34:29 +04:00
ret = max8998_write_reg ( i2c , reg , selector ) ;
max8998 - > buck2_vol [ max8998 - > buck2_idx ] = selector ;
2011-01-11 14:20:05 +03:00
buck2_gpio_set ( pdata - > buck2_set3 , max8998 - > buck2_idx ) ;
buck2_exit :
2010-09-27 16:32:27 +04:00
dev_dbg ( max8998 - > dev , " %s: SET3:%d \n " , i2c - > name ,
gpio_get_value ( pdata - > buck2_set3 ) ) ;
} else {
2012-06-17 05:34:29 +04:00
ret = max8998_write_reg ( i2c , reg , selector ) ;
2010-09-27 16:32:27 +04:00
}
break ;
case MAX8998_BUCK3 :
case MAX8998_BUCK4 :
2012-06-17 05:34:29 +04:00
ret = max8998_update_reg ( i2c , reg , selector < < shift ,
mask < < shift ) ;
2010-09-27 16:32:27 +04:00
break ;
2010-07-20 10:07:07 +04:00
}
2012-04-11 09:59:14 +04:00
return ret ;
}
static int max8998_set_voltage_buck_time_sel ( struct regulator_dev * rdev ,
unsigned int old_selector ,
unsigned int new_selector )
{
struct max8998_data * max8998 = rdev_get_drvdata ( rdev ) ;
struct i2c_client * i2c = max8998 - > iodev - > i2c ;
int buck = rdev_get_id ( rdev ) ;
u8 val = 0 ;
int difference , ret ;
if ( buck < MAX8998_BUCK1 | | buck > MAX8998_BUCK4 )
return - EINVAL ;
2010-09-27 16:32:27 +04:00
/* Voltage stabilization */
2012-04-11 09:59:14 +04:00
ret = max8998_read_reg ( i2c , MAX8998_REG_ONOFF4 , & val ) ;
if ( ret )
return ret ;
2010-09-27 16:32:27 +04:00
/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
/* MAX8998 has ENRAMP bit implemented, so test it*/
if ( max8998 - > iodev - > type = = TYPE_MAX8998 & & ! ( val & MAX8998_ENRAMP ) )
2012-04-11 09:59:14 +04:00
return 0 ;
2010-09-27 16:32:27 +04:00
2019-04-14 15:48:18 +03:00
difference = ( new_selector - old_selector ) * rdev - > desc - > uV_step / 1000 ;
2010-09-27 16:32:27 +04:00
if ( difference > 0 )
2013-01-09 15:34:57 +04:00
return DIV_ROUND_UP ( difference , ( val & 0x0f ) + 1 ) ;
2010-09-27 16:32:27 +04:00
2012-04-11 09:59:14 +04:00
return 0 ;
2010-06-16 11:04:16 +04:00
}
2019-04-14 15:48:17 +03:00
static const struct regulator_ops max8998_ldo_ops = {
2012-06-17 05:33:07 +04:00
. list_voltage = regulator_list_voltage_linear ,
2012-06-17 05:34:29 +04:00
. map_voltage = regulator_map_voltage_linear ,
2010-06-16 11:04:16 +04:00
. is_enabled = max8998_ldo_is_enabled ,
. enable = max8998_ldo_enable ,
. disable = max8998_ldo_disable ,
2012-04-11 09:58:09 +04:00
. get_voltage_sel = max8998_get_voltage_sel ,
2012-06-17 05:34:29 +04:00
. set_voltage_sel = max8998_set_voltage_ldo_sel ,
2010-06-16 11:04:16 +04:00
} ;
2019-04-14 15:48:17 +03:00
static const struct regulator_ops max8998_buck_ops = {
2012-06-17 05:33:07 +04:00
. list_voltage = regulator_list_voltage_linear ,
2012-06-17 05:34:29 +04:00
. map_voltage = regulator_map_voltage_linear ,
2010-06-16 11:04:16 +04:00
. is_enabled = max8998_ldo_is_enabled ,
. enable = max8998_ldo_enable ,
. disable = max8998_ldo_disable ,
2012-04-11 09:58:09 +04:00
. get_voltage_sel = max8998_get_voltage_sel ,
2012-06-17 05:34:29 +04:00
. set_voltage_sel = max8998_set_voltage_buck_sel ,
2012-04-11 09:59:14 +04:00
. set_voltage_time_sel = max8998_set_voltage_buck_time_sel ,
2010-06-16 11:04:16 +04:00
} ;
2019-04-14 15:48:17 +03:00
static const struct regulator_ops max8998_others_ops = {
2010-06-16 11:04:16 +04:00
. is_enabled = max8998_ldo_is_enabled ,
. enable = max8998_ldo_enable ,
. disable = max8998_ldo_disable ,
} ;
2019-04-14 15:48:18 +03:00
# define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \
{ \
. name = # _name , \
. id = MAX8998_ # # _name , \
. ops = _ops , \
. min_uV = ( _min ) , \
. uV_step = ( _step ) , \
. n_voltages = ( ( _max ) - ( _min ) ) / ( _step ) + 1 , \
. type = REGULATOR_VOLTAGE , \
. owner = THIS_MODULE , \
2010-06-16 11:04:16 +04:00
}
2019-04-14 15:48:18 +03:00
# define MAX8998_OTHERS_REG(_name, _id) \
{ \
. name = # _name , \
. id = _id , \
. ops = & max8998_others_ops , \
. type = REGULATOR_VOLTAGE , \
. owner = THIS_MODULE , \
}
static const struct regulator_desc regulators [ ] = {
MAX8998_LINEAR_REG ( LDO2 , & max8998_ldo_ops , 800000 , 50000 , 1300000 ) ,
MAX8998_LINEAR_REG ( LDO3 , & max8998_ldo_ops , 800000 , 50000 , 1300000 ) ,
MAX8998_LINEAR_REG ( LDO4 , & max8998_ldo_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( LDO5 , & max8998_ldo_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( LDO6 , & max8998_ldo_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( LDO7 , & max8998_ldo_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( LDO8 , & max8998_ldo_ops , 3000000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( LDO9 , & max8998_ldo_ops , 2800000 , 100000 , 3100000 ) ,
MAX8998_LINEAR_REG ( LDO10 , & max8998_ldo_ops , 950000 , 50000 , 1300000 ) ,
MAX8998_LINEAR_REG ( LDO11 , & max8998_ldo_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( LDO12 , & max8998_ldo_ops , 800000 , 100000 , 3300000 ) ,
MAX8998_LINEAR_REG ( LDO13 , & max8998_ldo_ops , 800000 , 100000 , 3300000 ) ,
MAX8998_LINEAR_REG ( LDO14 , & max8998_ldo_ops , 1200000 , 100000 , 3300000 ) ,
MAX8998_LINEAR_REG ( LDO15 , & max8998_ldo_ops , 1200000 , 100000 , 3300000 ) ,
MAX8998_LINEAR_REG ( LDO16 , & max8998_ldo_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( LDO17 , & max8998_ldo_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( BUCK1 , & max8998_buck_ops , 750000 , 25000 , 1525000 ) ,
MAX8998_LINEAR_REG ( BUCK2 , & max8998_buck_ops , 750000 , 25000 , 1525000 ) ,
MAX8998_LINEAR_REG ( BUCK3 , & max8998_buck_ops , 1600000 , 100000 , 3600000 ) ,
MAX8998_LINEAR_REG ( BUCK4 , & max8998_buck_ops , 800000 , 100000 , 2300000 ) ,
MAX8998_OTHERS_REG ( EN32KHz - AP , MAX8998_EN32KHZ_AP ) ,
MAX8998_OTHERS_REG ( EN32KHz - CP , MAX8998_EN32KHZ_CP ) ,
MAX8998_OTHERS_REG ( ENVICHG , MAX8998_ENVICHG ) ,
MAX8998_OTHERS_REG ( ESAFEOUT1 , MAX8998_ESAFEOUT1 ) ,
MAX8998_OTHERS_REG ( ESAFEOUT2 , MAX8998_ESAFEOUT2 ) ,
2010-06-16 11:04:16 +04:00
} ;
2013-06-25 18:08:10 +04:00
static int max8998_pmic_dt_parse_dvs_gpio ( struct max8998_dev * iodev ,
struct max8998_platform_data * pdata ,
struct device_node * pmic_np )
{
int gpio ;
gpio = of_get_named_gpio ( pmic_np , " max8998,pmic-buck1-dvs-gpios " , 0 ) ;
if ( ! gpio_is_valid ( gpio ) ) {
dev_err ( iodev - > dev , " invalid buck1 gpio[0]: %d \n " , gpio ) ;
return - EINVAL ;
}
pdata - > buck1_set1 = gpio ;
gpio = of_get_named_gpio ( pmic_np , " max8998,pmic-buck1-dvs-gpios " , 1 ) ;
if ( ! gpio_is_valid ( gpio ) ) {
dev_err ( iodev - > dev , " invalid buck1 gpio[1]: %d \n " , gpio ) ;
return - EINVAL ;
}
pdata - > buck1_set2 = gpio ;
gpio = of_get_named_gpio ( pmic_np , " max8998,pmic-buck2-dvs-gpio " , 0 ) ;
if ( ! gpio_is_valid ( gpio ) ) {
dev_err ( iodev - > dev , " invalid buck 2 gpio: %d \n " , gpio ) ;
return - EINVAL ;
}
pdata - > buck2_set3 = gpio ;
return 0 ;
}
static int max8998_pmic_dt_parse_pdata ( struct max8998_dev * iodev ,
struct max8998_platform_data * pdata )
{
struct device_node * pmic_np = iodev - > dev - > of_node ;
struct device_node * regulators_np , * reg_np ;
struct max8998_regulator_data * rdata ;
unsigned int i ;
int ret ;
regulators_np = of_get_child_by_name ( pmic_np , " regulators " ) ;
if ( ! regulators_np ) {
dev_err ( iodev - > dev , " could not find regulators sub-node \n " ) ;
return - EINVAL ;
}
/* count the number of regulators to be supported in pmic */
pdata - > num_regulators = of_get_child_count ( regulators_np ) ;
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 00:07:58 +03:00
rdata = devm_kcalloc ( iodev - > dev ,
pdata - > num_regulators , sizeof ( * rdata ) ,
GFP_KERNEL ) ;
2014-02-17 13:03:35 +04:00
if ( ! rdata ) {
of_node_put ( regulators_np ) ;
2013-06-25 18:08:10 +04:00
return - ENOMEM ;
2014-02-17 13:03:35 +04:00
}
2013-06-25 18:08:10 +04:00
pdata - > regulators = rdata ;
for ( i = 0 ; i < ARRAY_SIZE ( regulators ) ; + + i ) {
reg_np = of_get_child_by_name ( regulators_np ,
regulators [ i ] . name ) ;
if ( ! reg_np )
continue ;
rdata - > id = regulators [ i ] . id ;
2014-11-10 16:43:53 +03:00
rdata - > initdata = of_get_regulator_init_data ( iodev - > dev ,
reg_np ,
& regulators [ i ] ) ;
2013-06-25 18:08:10 +04:00
rdata - > reg_node = reg_np ;
+ + rdata ;
}
pdata - > num_regulators = rdata - pdata - > regulators ;
2014-02-17 13:03:35 +04:00
of_node_put ( reg_np ) ;
of_node_put ( regulators_np ) ;
2013-06-25 18:08:10 +04:00
ret = max8998_pmic_dt_parse_dvs_gpio ( iodev , pdata , pmic_np ) ;
if ( ret )
return - EINVAL ;
if ( of_find_property ( pmic_np , " max8998,pmic-buck-voltage-lock " , NULL ) )
pdata - > buck_voltage_lock = true ;
ret = of_property_read_u32 ( pmic_np ,
" max8998,pmic-buck1-default-dvs-idx " ,
& pdata - > buck1_default_idx ) ;
if ( ! ret & & pdata - > buck1_default_idx > = 4 ) {
pdata - > buck1_default_idx = 0 ;
dev_warn ( iodev - > dev , " invalid value for default dvs index, using 0 instead \n " ) ;
}
ret = of_property_read_u32 ( pmic_np ,
" max8998,pmic-buck2-default-dvs-idx " ,
& pdata - > buck2_default_idx ) ;
if ( ! ret & & pdata - > buck2_default_idx > = 2 ) {
pdata - > buck2_default_idx = 0 ;
dev_warn ( iodev - > dev , " invalid value for default dvs index, using 0 instead \n " ) ;
}
ret = of_property_read_u32_array ( pmic_np ,
" max8998,pmic-buck1-dvs-voltage " ,
pdata - > buck1_voltage ,
ARRAY_SIZE ( pdata - > buck1_voltage ) ) ;
if ( ret ) {
dev_err ( iodev - > dev , " buck1 voltages not specified \n " ) ;
return - EINVAL ;
}
ret = of_property_read_u32_array ( pmic_np ,
" max8998,pmic-buck2-dvs-voltage " ,
pdata - > buck2_voltage ,
ARRAY_SIZE ( pdata - > buck2_voltage ) ) ;
if ( ret ) {
dev_err ( iodev - > dev , " buck2 voltages not specified \n " ) ;
return - EINVAL ;
}
return 0 ;
}
2012-11-19 22:22:22 +04:00
static int max8998_pmic_probe ( struct platform_device * pdev )
2010-06-16 11:04:16 +04:00
{
struct max8998_dev * iodev = dev_get_drvdata ( pdev - > dev . parent ) ;
2013-06-25 18:08:10 +04:00
struct max8998_platform_data * pdata = iodev - > pdata ;
2012-04-04 03:50:22 +04:00
struct regulator_config config = { } ;
2014-03-09 11:32:19 +04:00
struct regulator_dev * rdev ;
2010-06-16 11:04:16 +04:00
struct max8998_data * max8998 ;
2010-09-27 16:32:27 +04:00
struct i2c_client * i2c ;
2014-03-09 11:32:19 +04:00
int i , ret ;
2013-06-24 16:39:53 +04:00
unsigned int v ;
2010-06-16 11:04:16 +04:00
if ( ! pdata ) {
dev_err ( pdev - > dev . parent , " No platform init data supplied \n " ) ;
return - ENODEV ;
}
2013-06-25 18:08:10 +04:00
if ( IS_ENABLED ( CONFIG_OF ) & & iodev - > dev - > of_node ) {
ret = max8998_pmic_dt_parse_pdata ( iodev , pdata ) ;
if ( ret )
return ret ;
}
2012-04-19 05:47:37 +04:00
max8998 = devm_kzalloc ( & pdev - > dev , sizeof ( struct max8998_data ) ,
GFP_KERNEL ) ;
2010-06-16 11:04:16 +04:00
if ( ! max8998 )
return - ENOMEM ;
2010-08-27 12:37:34 +04:00
max8998 - > dev = & pdev - > dev ;
2010-06-16 11:04:16 +04:00
max8998 - > iodev = iodev ;
2010-08-22 11:26:49 +04:00
max8998 - > num_regulators = pdata - > num_regulators ;
2010-06-16 11:04:16 +04:00
platform_set_drvdata ( pdev , max8998 ) ;
2010-09-27 16:32:27 +04:00
i2c = max8998 - > iodev - > i2c ;
2011-01-11 14:20:05 +03:00
max8998 - > buck1_idx = pdata - > buck1_default_idx ;
max8998 - > buck2_idx = pdata - > buck2_default_idx ;
2010-09-27 16:32:27 +04:00
/* NOTE: */
/* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
/* will be displayed */
/* Check if MAX8998 voltage selection GPIOs are defined */
if ( gpio_is_valid ( pdata - > buck1_set1 ) & &
gpio_is_valid ( pdata - > buck1_set2 ) ) {
/* Check if SET1 is not equal to 0 */
if ( ! pdata - > buck1_set1 ) {
2013-02-23 07:52:35 +04:00
dev_err ( & pdev - > dev ,
" MAX8998 SET1 GPIO defined as 0 ! \n " ) ;
2010-09-27 16:32:27 +04:00
WARN_ON ( ! pdata - > buck1_set1 ) ;
2013-09-04 09:38:02 +04:00
return - EIO ;
2010-09-27 16:32:27 +04:00
}
/* Check if SET2 is not equal to 0 */
if ( ! pdata - > buck1_set2 ) {
2013-02-23 07:52:35 +04:00
dev_err ( & pdev - > dev ,
" MAX8998 SET2 GPIO defined as 0 ! \n " ) ;
2010-09-27 16:32:27 +04:00
WARN_ON ( ! pdata - > buck1_set2 ) ;
2013-09-04 09:38:02 +04:00
return - EIO ;
2010-09-27 16:32:27 +04:00
}
gpio_request ( pdata - > buck1_set1 , " MAX8998 BUCK1_SET1 " ) ;
gpio_direction_output ( pdata - > buck1_set1 ,
max8998 - > buck1_idx & 0x1 ) ;
gpio_request ( pdata - > buck1_set2 , " MAX8998 BUCK1_SET2 " ) ;
gpio_direction_output ( pdata - > buck1_set2 ,
( max8998 - > buck1_idx > > 1 ) & 0x1 ) ;
2013-06-24 16:39:53 +04:00
/* Set predefined values for BUCK1 registers */
for ( v = 0 ; v < ARRAY_SIZE ( pdata - > buck1_voltage ) ; + + v ) {
2019-04-14 15:48:18 +03:00
int index = MAX8998_BUCK1 - MAX8998_LDO2 ;
2013-06-24 16:39:53 +04:00
i = 0 ;
2019-04-14 15:48:18 +03:00
while ( regulators [ index ] . min_uV +
regulators [ index ] . uV_step * i
2013-06-24 16:39:53 +04:00
< pdata - > buck1_voltage [ v ] )
i + + ;
max8998 - > buck1_vol [ v ] = i ;
ret = max8998_write_reg ( i2c ,
MAX8998_REG_BUCK1_VOLTAGE1 + v , i ) ;
if ( ret )
2013-09-04 09:38:02 +04:00
return ret ;
2013-06-24 16:39:53 +04:00
}
2010-09-27 16:32:27 +04:00
}
if ( gpio_is_valid ( pdata - > buck2_set3 ) ) {
/* Check if SET3 is not equal to 0 */
if ( ! pdata - > buck2_set3 ) {
2013-02-23 07:52:35 +04:00
dev_err ( & pdev - > dev ,
" MAX8998 SET3 GPIO defined as 0 ! \n " ) ;
2010-09-27 16:32:27 +04:00
WARN_ON ( ! pdata - > buck2_set3 ) ;
2013-09-04 09:38:02 +04:00
return - EIO ;
2010-09-27 16:32:27 +04:00
}
gpio_request ( pdata - > buck2_set3 , " MAX8998 BUCK2_SET3 " ) ;
gpio_direction_output ( pdata - > buck2_set3 ,
max8998 - > buck2_idx & 0x1 ) ;
2013-06-24 16:39:53 +04:00
/* Set predefined values for BUCK2 registers */
for ( v = 0 ; v < ARRAY_SIZE ( pdata - > buck2_voltage ) ; + + v ) {
2019-04-14 15:48:18 +03:00
int index = MAX8998_BUCK2 - MAX8998_LDO2 ;
2013-06-24 16:39:53 +04:00
i = 0 ;
2019-04-14 15:48:18 +03:00
while ( regulators [ index ] . min_uV +
regulators [ index ] . uV_step * i
2013-06-24 16:39:53 +04:00
< pdata - > buck2_voltage [ v ] )
i + + ;
max8998 - > buck2_vol [ v ] = i ;
ret = max8998_write_reg ( i2c ,
MAX8998_REG_BUCK2_VOLTAGE1 + v , i ) ;
if ( ret )
2013-09-04 09:38:02 +04:00
return ret ;
2013-06-24 16:39:53 +04:00
}
2010-09-27 16:32:27 +04:00
}
2010-06-16 11:04:16 +04:00
for ( i = 0 ; i < pdata - > num_regulators ; i + + ) {
2019-04-14 15:48:18 +03:00
int index = pdata - > regulators [ i ] . id - MAX8998_LDO2 ;
2012-04-04 03:50:22 +04:00
config . dev = max8998 - > dev ;
2013-06-25 18:08:10 +04:00
config . of_node = pdata - > regulators [ i ] . reg_node ;
2012-04-04 03:50:22 +04:00
config . init_data = pdata - > regulators [ i ] . initdata ;
config . driver_data = max8998 ;
2014-03-09 11:32:19 +04:00
rdev = devm_regulator_register ( & pdev - > dev , & regulators [ index ] ,
& config ) ;
if ( IS_ERR ( rdev ) ) {
ret = PTR_ERR ( rdev ) ;
2013-06-25 18:08:10 +04:00
dev_err ( max8998 - > dev , " regulator %s init failed (%d) \n " ,
regulators [ index ] . name , ret ) ;
2013-09-04 09:38:02 +04:00
return ret ;
2010-06-16 11:04:16 +04:00
}
}
return 0 ;
}
2011-01-04 08:17:39 +03:00
static const struct platform_device_id max8998_pmic_id [ ] = {
{ " max8998-pmic " , TYPE_MAX8998 } ,
{ " lp3974-pmic " , TYPE_LP3974 } ,
{ }
} ;
2011-03-26 18:28:42 +03:00
MODULE_DEVICE_TABLE ( platform , max8998_pmic_id ) ;
2011-01-04 08:17:39 +03:00
2010-06-16 11:04:16 +04:00
static struct platform_driver max8998_pmic_driver = {
. driver = {
. name = " max8998-pmic " ,
} ,
. probe = max8998_pmic_probe ,
2011-01-04 08:17:39 +03:00
. id_table = max8998_pmic_id ,
2010-06-16 11:04:16 +04:00
} ;
static int __init max8998_pmic_init ( void )
{
return platform_driver_register ( & max8998_pmic_driver ) ;
}
subsys_initcall ( max8998_pmic_init ) ;
static void __exit max8998_pmic_cleanup ( void )
{
platform_driver_unregister ( & max8998_pmic_driver ) ;
}
module_exit ( max8998_pmic_cleanup ) ;
MODULE_DESCRIPTION ( " MAXIM 8998 voltage regulator driver " ) ;
MODULE_AUTHOR ( " Kyungmin Park <kyungmin.park@samsung.com> " ) ;
MODULE_LICENSE ( " GPL " ) ;