1*4882a593Smuzhiyun================================== 2*4882a593SmuzhiyunPMBus core driver and internal API 3*4882a593Smuzhiyun================================== 4*4882a593Smuzhiyun 5*4882a593SmuzhiyunIntroduction 6*4882a593Smuzhiyun============ 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun[from pmbus.org] The Power Management Bus (PMBus) is an open standard 9*4882a593Smuzhiyunpower-management protocol with a fully defined command language that facilitates 10*4882a593Smuzhiyuncommunication with power converters and other devices in a power system. The 11*4882a593Smuzhiyunprotocol is implemented over the industry-standard SMBus serial interface and 12*4882a593Smuzhiyunenables programming, control, and real-time monitoring of compliant power 13*4882a593Smuzhiyunconversion products. This flexible and highly versatile standard allows for 14*4882a593Smuzhiyuncommunication between devices based on both analog and digital technologies, and 15*4882a593Smuzhiyunprovides true interoperability which will reduce design complexity and shorten 16*4882a593Smuzhiyuntime to market for power system designers. Pioneered by leading power supply and 17*4882a593Smuzhiyunsemiconductor companies, this open power system standard is maintained and 18*4882a593Smuzhiyunpromoted by the PMBus Implementers Forum (PMBus-IF), comprising 30+ adopters 19*4882a593Smuzhiyunwith the objective to provide support to, and facilitate adoption among, users. 20*4882a593Smuzhiyun 21*4882a593SmuzhiyunUnfortunately, while PMBus commands are standardized, there are no mandatory 22*4882a593Smuzhiyuncommands, and manufacturers can add as many non-standard commands as they like. 23*4882a593SmuzhiyunAlso, different PMBUs devices act differently if non-supported commands are 24*4882a593Smuzhiyunexecuted. Some devices return an error, some devices return 0xff or 0xffff and 25*4882a593Smuzhiyunset a status error flag, and some devices may simply hang up. 26*4882a593Smuzhiyun 27*4882a593SmuzhiyunDespite all those difficulties, a generic PMBus device driver is still useful 28*4882a593Smuzhiyunand supported since kernel version 2.6.39. However, it was necessary to support 29*4882a593Smuzhiyundevice specific extensions in addition to the core PMBus driver, since it is 30*4882a593Smuzhiyunsimply unknown what new device specific functionality PMBus device developers 31*4882a593Smuzhiyuncome up with next. 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunTo make device specific extensions as scalable as possible, and to avoid having 34*4882a593Smuzhiyunto modify the core PMBus driver repeatedly for new devices, the PMBus driver was 35*4882a593Smuzhiyunsplit into core, generic, and device specific code. The core code (in 36*4882a593Smuzhiyunpmbus_core.c) provides generic functionality. The generic code (in pmbus.c) 37*4882a593Smuzhiyunprovides support for generic PMBus devices. Device specific code is responsible 38*4882a593Smuzhiyunfor device specific initialization and, if needed, maps device specific 39*4882a593Smuzhiyunfunctionality into generic functionality. This is to some degree comparable 40*4882a593Smuzhiyunto PCI code, where generic code is augmented as needed with quirks for all kinds 41*4882a593Smuzhiyunof devices. 42*4882a593Smuzhiyun 43*4882a593SmuzhiyunPMBus device capabilities auto-detection 44*4882a593Smuzhiyun======================================== 45*4882a593Smuzhiyun 46*4882a593SmuzhiyunFor generic PMBus devices, code in pmbus.c attempts to auto-detect all supported 47*4882a593SmuzhiyunPMBus commands. Auto-detection is somewhat limited, since there are simply too 48*4882a593Smuzhiyunmany variables to consider. For example, it is almost impossible to autodetect 49*4882a593Smuzhiyunwhich PMBus commands are paged and which commands are replicated across all 50*4882a593Smuzhiyunpages (see the PMBus specification for details on multi-page PMBus devices). 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunFor this reason, it often makes sense to provide a device specific driver if not 53*4882a593Smuzhiyunall commands can be auto-detected. The data structures in this driver can be 54*4882a593Smuzhiyunused to inform the core driver about functionality supported by individual 55*4882a593Smuzhiyunchips. 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunSome commands are always auto-detected. This applies to all limit commands 58*4882a593Smuzhiyun(lcrit, min, max, and crit attributes) as well as associated alarm attributes. 59*4882a593SmuzhiyunLimits and alarm attributes are auto-detected because there are simply too many 60*4882a593Smuzhiyunpossible combinations to provide a manual configuration interface. 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunPMBus internal API 63*4882a593Smuzhiyun================== 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunThe API between core and device specific PMBus code is defined in 66*4882a593Smuzhiyundrivers/hwmon/pmbus/pmbus.h. In addition to the internal API, pmbus.h defines 67*4882a593Smuzhiyunstandard PMBus commands and virtual PMBus commands. 68*4882a593Smuzhiyun 69*4882a593SmuzhiyunStandard PMBus commands 70*4882a593Smuzhiyun----------------------- 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunStandard PMBus commands (commands values 0x00 to 0xff) are defined in the PMBUs 73*4882a593Smuzhiyunspecification. 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunVirtual PMBus commands 76*4882a593Smuzhiyun---------------------- 77*4882a593Smuzhiyun 78*4882a593SmuzhiyunVirtual PMBus commands are provided to enable support for non-standard 79*4882a593Smuzhiyunfunctionality which has been implemented by several chip vendors and is thus 80*4882a593Smuzhiyundesirable to support. 81*4882a593Smuzhiyun 82*4882a593SmuzhiyunVirtual PMBus commands start with command value 0x100 and can thus easily be 83*4882a593Smuzhiyundistinguished from standard PMBus commands (which can not have values larger 84*4882a593Smuzhiyunthan 0xff). Support for virtual PMBus commands is device specific and thus has 85*4882a593Smuzhiyunto be implemented in device specific code. 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunVirtual commands are named PMBUS_VIRT_xxx and start with PMBUS_VIRT_BASE. All 88*4882a593Smuzhiyunvirtual commands are word sized. 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunThere are currently two types of virtual commands. 91*4882a593Smuzhiyun 92*4882a593Smuzhiyun- READ commands are read-only; writes are either ignored or return an error. 93*4882a593Smuzhiyun- RESET commands are read/write. Reading reset registers returns zero 94*4882a593Smuzhiyun (used for detection), writing any value causes the associated history to be 95*4882a593Smuzhiyun reset. 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunVirtual commands have to be handled in device specific driver code. Chip driver 98*4882a593Smuzhiyuncode returns non-negative values if a virtual command is supported, or a 99*4882a593Smuzhiyunnegative error code if not. The chip driver may return -ENODATA or any other 100*4882a593SmuzhiyunLinux error code in this case, though an error code other than -ENODATA is 101*4882a593Smuzhiyunhandled more efficiently and thus preferred. Either case, the calling PMBus 102*4882a593Smuzhiyuncore code will abort if the chip driver returns an error code when reading 103*4882a593Smuzhiyunor writing virtual registers (in other words, the PMBus core code will never 104*4882a593Smuzhiyunsend a virtual command to a chip). 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunPMBus driver information 107*4882a593Smuzhiyun------------------------ 108*4882a593Smuzhiyun 109*4882a593SmuzhiyunPMBus driver information, defined in struct pmbus_driver_info, is the main means 110*4882a593Smuzhiyunfor device specific drivers to pass information to the core PMBus driver. 111*4882a593SmuzhiyunSpecifically, it provides the following information. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun- For devices supporting its data in Direct Data Format, it provides coefficients 114*4882a593Smuzhiyun for converting register values into normalized data. This data is usually 115*4882a593Smuzhiyun provided by chip manufacturers in device datasheets. 116*4882a593Smuzhiyun- Supported chip functionality can be provided to the core driver. This may be 117*4882a593Smuzhiyun necessary for chips which react badly if non-supported commands are executed, 118*4882a593Smuzhiyun and/or to speed up device detection and initialization. 119*4882a593Smuzhiyun- Several function entry points are provided to support overriding and/or 120*4882a593Smuzhiyun augmenting generic command execution. This functionality can be used to map 121*4882a593Smuzhiyun non-standard PMBus commands to standard commands, or to augment standard 122*4882a593Smuzhiyun command return values with device specific information. 123*4882a593Smuzhiyun 124*4882a593SmuzhiyunAPI functions 125*4882a593Smuzhiyun============= 126*4882a593Smuzhiyun 127*4882a593SmuzhiyunFunctions provided by chip driver 128*4882a593Smuzhiyun--------------------------------- 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunAll functions return the command return value (read) or zero (write) if 131*4882a593Smuzhiyunsuccessful. A return value of -ENODATA indicates that there is no manufacturer 132*4882a593Smuzhiyunspecific command, but that a standard PMBus command may exist. Any other 133*4882a593Smuzhiyunnegative return value indicates that the commands does not exist for this 134*4882a593Smuzhiyunchip, and that no attempt should be made to read or write the standard 135*4882a593Smuzhiyuncommand. 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunAs mentioned above, an exception to this rule applies to virtual commands, 138*4882a593Smuzhiyunwhich *must* be handled in driver specific code. See "Virtual PMBus Commands" 139*4882a593Smuzhiyunabove for more details. 140*4882a593Smuzhiyun 141*4882a593SmuzhiyunCommand execution in the core PMBus driver code is as follows:: 142*4882a593Smuzhiyun 143*4882a593Smuzhiyun if (chip_access_function) { 144*4882a593Smuzhiyun status = chip_access_function(); 145*4882a593Smuzhiyun if (status != -ENODATA) 146*4882a593Smuzhiyun return status; 147*4882a593Smuzhiyun } 148*4882a593Smuzhiyun if (command >= PMBUS_VIRT_BASE) /* For word commands/registers only */ 149*4882a593Smuzhiyun return -EINVAL; 150*4882a593Smuzhiyun return generic_access(); 151*4882a593Smuzhiyun 152*4882a593SmuzhiyunChip drivers may provide pointers to the following functions in struct 153*4882a593Smuzhiyunpmbus_driver_info. All functions are optional. 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun:: 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun int (*read_byte_data)(struct i2c_client *client, int page, int reg); 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunRead byte from page <page>, register <reg>. 160*4882a593Smuzhiyun<page> may be -1, which means "current page". 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun 163*4882a593Smuzhiyun:: 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun int (*read_word_data)(struct i2c_client *client, int page, int phase, 166*4882a593Smuzhiyun int reg); 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunRead word from page <page>, phase <pase>, register <reg>. If the chip does not 169*4882a593Smuzhiyunsupport multiple phases, the phase parameter can be ignored. If the chip 170*4882a593Smuzhiyunsupports multiple phases, a phase value of 0xff indicates all phases. 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun:: 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun int (*write_word_data)(struct i2c_client *client, int page, int reg, 175*4882a593Smuzhiyun u16 word); 176*4882a593Smuzhiyun 177*4882a593SmuzhiyunWrite word to page <page>, register <reg>. 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun:: 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun int (*write_byte)(struct i2c_client *client, int page, u8 value); 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunWrite byte to page <page>, register <reg>. 184*4882a593Smuzhiyun<page> may be -1, which means "current page". 185*4882a593Smuzhiyun 186*4882a593Smuzhiyun:: 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun int (*identify)(struct i2c_client *client, struct pmbus_driver_info *info); 189*4882a593Smuzhiyun 190*4882a593SmuzhiyunDetermine supported PMBus functionality. This function is only necessary 191*4882a593Smuzhiyunif a chip driver supports multiple chips, and the chip functionality is not 192*4882a593Smuzhiyunpre-determined. It is currently only used by the generic pmbus driver 193*4882a593Smuzhiyun(pmbus.c). 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunFunctions exported by core driver 196*4882a593Smuzhiyun--------------------------------- 197*4882a593Smuzhiyun 198*4882a593SmuzhiyunChip drivers are expected to use the following functions to read or write 199*4882a593SmuzhiyunPMBus registers. Chip drivers may also use direct I2C commands. If direct I2C 200*4882a593Smuzhiyuncommands are used, the chip driver code must not directly modify the current 201*4882a593Smuzhiyunpage, since the selected page is cached in the core driver and the core driver 202*4882a593Smuzhiyunwill assume that it is selected. Using pmbus_set_page() to select a new page 203*4882a593Smuzhiyunis mandatory. 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun:: 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun int pmbus_set_page(struct i2c_client *client, u8 page, u8 phase); 208*4882a593Smuzhiyun 209*4882a593SmuzhiyunSet PMBus page register to <page> and <phase> for subsequent commands. 210*4882a593SmuzhiyunIf the chip does not support multiple phases, the phase parameter is 211*4882a593Smuzhiyunignored. Otherwise, a phase value of 0xff selects all phases. 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun:: 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 phase, 216*4882a593Smuzhiyun u8 reg); 217*4882a593Smuzhiyun 218*4882a593SmuzhiyunRead word data from <page>, <phase>, <reg>. Similar to 219*4882a593Smuzhiyuni2c_smbus_read_word_data(), but selects page and phase first. If the chip does 220*4882a593Smuzhiyunnot support multiple phases, the phase parameter is ignored. Otherwise, a phase 221*4882a593Smuzhiyunvalue of 0xff selects all phases. 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun:: 224*4882a593Smuzhiyun 225*4882a593Smuzhiyun int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, 226*4882a593Smuzhiyun u16 word); 227*4882a593Smuzhiyun 228*4882a593SmuzhiyunWrite word data to <page>, <reg>. Similar to i2c_smbus_write_word_data(), but 229*4882a593Smuzhiyunselects page first. 230*4882a593Smuzhiyun 231*4882a593Smuzhiyun:: 232*4882a593Smuzhiyun 233*4882a593Smuzhiyun int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg); 234*4882a593Smuzhiyun 235*4882a593SmuzhiyunRead byte data from <page>, <reg>. Similar to i2c_smbus_read_byte_data(), but 236*4882a593Smuzhiyunselects page first. <page> may be -1, which means "current page". 237*4882a593Smuzhiyun 238*4882a593Smuzhiyun:: 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun int pmbus_write_byte(struct i2c_client *client, int page, u8 value); 241*4882a593Smuzhiyun 242*4882a593SmuzhiyunWrite byte data to <page>, <reg>. Similar to i2c_smbus_write_byte(), but 243*4882a593Smuzhiyunselects page first. <page> may be -1, which means "current page". 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun:: 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun void pmbus_clear_faults(struct i2c_client *client); 248*4882a593Smuzhiyun 249*4882a593SmuzhiyunExecute PMBus "Clear Fault" command on all chip pages. 250*4882a593SmuzhiyunThis function calls the device specific write_byte function if defined. 251*4882a593SmuzhiyunTherefore, it must _not_ be called from that function. 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun:: 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg); 256*4882a593Smuzhiyun 257*4882a593SmuzhiyunCheck if byte register exists. Return true if the register exists, false 258*4882a593Smuzhiyunotherwise. 259*4882a593SmuzhiyunThis function calls the device specific write_byte function if defined to 260*4882a593Smuzhiyunobtain the chip status. Therefore, it must _not_ be called from that function. 261*4882a593Smuzhiyun 262*4882a593Smuzhiyun:: 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun bool pmbus_check_word_register(struct i2c_client *client, int page, int reg); 265*4882a593Smuzhiyun 266*4882a593SmuzhiyunCheck if word register exists. Return true if the register exists, false 267*4882a593Smuzhiyunotherwise. 268*4882a593SmuzhiyunThis function calls the device specific write_byte function if defined to 269*4882a593Smuzhiyunobtain the chip status. Therefore, it must _not_ be called from that function. 270*4882a593Smuzhiyun 271*4882a593Smuzhiyun:: 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info); 274*4882a593Smuzhiyun 275*4882a593SmuzhiyunExecute probe function. Similar to standard probe function for other drivers, 276*4882a593Smuzhiyunwith the pointer to struct pmbus_driver_info as additional argument. Calls 277*4882a593Smuzhiyunidentify function if supported. Must only be called from device probe 278*4882a593Smuzhiyunfunction. 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun:: 281*4882a593Smuzhiyun 282*4882a593Smuzhiyun void pmbus_do_remove(struct i2c_client *client); 283*4882a593Smuzhiyun 284*4882a593SmuzhiyunExecute driver remove function. Similar to standard driver remove function. 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun:: 287*4882a593Smuzhiyun 288*4882a593Smuzhiyun const struct pmbus_driver_info 289*4882a593Smuzhiyun *pmbus_get_driver_info(struct i2c_client *client); 290*4882a593Smuzhiyun 291*4882a593SmuzhiyunReturn pointer to struct pmbus_driver_info as passed to pmbus_do_probe(). 292*4882a593Smuzhiyun 293*4882a593Smuzhiyun 294*4882a593SmuzhiyunPMBus driver platform data 295*4882a593Smuzhiyun========================== 296*4882a593Smuzhiyun 297*4882a593SmuzhiyunPMBus platform data is defined in include/linux/pmbus.h. Platform data 298*4882a593Smuzhiyuncurrently only provides a flag field with a single bit used:: 299*4882a593Smuzhiyun 300*4882a593Smuzhiyun #define PMBUS_SKIP_STATUS_CHECK (1 << 0) 301*4882a593Smuzhiyun 302*4882a593Smuzhiyun struct pmbus_platform_data { 303*4882a593Smuzhiyun u32 flags; /* Device specific flags */ 304*4882a593Smuzhiyun }; 305*4882a593Smuzhiyun 306*4882a593Smuzhiyun 307*4882a593SmuzhiyunFlags 308*4882a593Smuzhiyun----- 309*4882a593Smuzhiyun 310*4882a593SmuzhiyunPMBUS_SKIP_STATUS_CHECK 311*4882a593Smuzhiyun During register detection, skip checking the status register for 312*4882a593Smuzhiyun communication or command errors. 313*4882a593Smuzhiyun 314*4882a593SmuzhiyunSome PMBus chips respond with valid data when trying to read an unsupported 315*4882a593Smuzhiyunregister. For such chips, checking the status register is mandatory when 316*4882a593Smuzhiyuntrying to determine if a chip register exists or not. 317*4882a593SmuzhiyunOther PMBus chips don't support the STATUS_CML register, or report 318*4882a593Smuzhiyuncommunication errors for no explicable reason. For such chips, checking the 319*4882a593Smuzhiyunstatus register must be disabled. 320*4882a593Smuzhiyun 321*4882a593SmuzhiyunSome i2c controllers do not support single-byte commands (write commands with 322*4882a593Smuzhiyunno data, i2c_smbus_write_byte()). With such controllers, clearing the status 323*4882a593Smuzhiyunregister is impossible, and the PMBUS_SKIP_STATUS_CHECK flag must be set. 324