Parallel Port Devices

parport_register_driver

parport_register_driver(driver)

register a parallel port device driver

Parameters

driver

structure describing the driver

Description

This can be called by a parallel port device driver in orderto receive notifications about ports being found in thesystem, as well as ports no longer available.

Thedriver structure is allocated by the caller and must not bedeallocated until after callingparport_unregister_driver().

If using the non device model:The driver’sattach() function may block. The port thatattach() is given will be valid for the duration of thecallback, but if the driver wants to take a copy of thepointer it must callparport_get_port() to do so. Callingparport_register_device() on that port will do this for you.

The driver’sdetach() function may block. The port thatdetach() is given will be valid for the duration of thecallback, but if the driver wants to take a copy of thepointer it must callparport_get_port() to do so.

Returns 0 on success. The non device model will always succeeds.but the new device model can fail and will return the error code.

module_parport_driver

module_parport_driver(__parport_driver)

Helper macro for registering a modular parport driver

Parameters

__parport_driver

structparport_driver to be used

Description

Helper macro for parport drivers which do not do anything special in moduleinit and exit. This eliminates a lot of boilerplate. Each module may onlyuse this macro once, and calling it replacesmodule_init() andmodule_exit().

intparport_yield(structpardevice*dev)

relinquish a parallel port temporarily

Parameters

structpardevice*dev

a device on the parallel port

Description

This function relinquishes the port if it would be helpful to otherdrivers to do so. Afterwards it tries to reclaim the port usingparport_claim(), and the return value is the same as forparport_claim(). If it fails, the port is left unclaimed and it isthe driver’s responsibility to reclaim the port.

Theparport_yield() andparport_yield_blocking() functions are formarking points in the driver at which other drivers may claim theport and use their devices. Yielding the port is similar toreleasing it and reclaiming it, but is more efficient because noaction is taken if there are no other devices needing the port. Infact, nothing is done even if there are other devices waiting butthe current device is still within its “timeslice”. The defaulttimeslice is half a second, but it can be adjusted via the /procinterface.

intparport_yield_blocking(structpardevice*dev)

relinquish a parallel port temporarily

Parameters

structpardevice*dev

a device on the parallel port

Description

This function relinquishes the port if it would be helpful to otherdrivers to do so. Afterwards it tries to reclaim the port usingparport_claim_or_block(), and the return value is the same as forparport_claim_or_block().

intparport_wait_event(structparport*port,signedlongtimeout)

wait for an event on a parallel port

Parameters

structparport*port

port to wait on

signedlongtimeout

time to wait (in jiffies)

Description

This function waits for up totimeout jiffies for aninterrupt to occur on a parallel port. If the port timeout isset to zero, it returns immediately.

If an interrupt occurs before the timeout period elapses, thisfunction returns zero immediately. If it times out, it returnsone. An error code less than zero indicates an error (mostlikely a pending signal), and the calling code should finishwhat it’s doing as soon as it can.

intparport_wait_peripheral(structparport*port,unsignedcharmask,unsignedcharresult)

wait for status lines to change in 35ms

Parameters

structparport*port

port to watch

unsignedcharmask

status lines to watch

unsignedcharresult

desired values of chosen status lines

Description

This function waits until the masked status lines have thedesired values, or until 35ms have elapsed (see IEEE 1284-1994page 24 to 25 for why this value in particular is hardcoded).Themask andresult parameters are bitmasks, with the bitsdefined by the constants in parport.h:PARPORT_STATUS_BUSY,and so on.

The port is polled quickly to start off with, in anticipationof a fast response from the peripheral. This fast pollingtime is configurable (using /proc), and defaults to 500usec.If the timeout for this port (seeparport_set_timeout()) iszero, the fast polling time is 35ms, and this function doesnot callschedule().

If the timeout for this port is non-zero, after the fastpolling fails it usesparport_wait_event() to wait for up to10ms, waking up if an interrupt occurs.

intparport_negotiate(structparport*port,intmode)

negotiate an IEEE 1284 mode

Parameters

structparport*port

port to use

intmode

mode to negotiate to

Description

Use this to negotiate to a particular IEEE 1284 transfer mode.Themode parameter should be one of the constants inparport.h startingIEEE1284_MODE_xxx.

The return value is 0 if the peripheral has accepted thenegotiation to the mode specified, -1 if the peripheral is notIEEE 1284 compliant (or not present), or 1 if the peripheralhas rejected the negotiation.

ssize_tparport_write(structparport*port,constvoid*buffer,size_tlen)

write a block of data to a parallel port

Parameters

structparport*port

port to write to

constvoid*buffer

data buffer (in kernel space)

size_tlen

number of bytes of data to transfer

Description

This will write up tolen bytes ofbuffer to the portspecified, using the IEEE 1284 transfer mode most recentlynegotiated to (usingparport_negotiate()), as long as thatmode supports forward transfers (host to peripheral).

It is the caller’s responsibility to ensure that the firstlen bytes ofbuffer are valid.

This function returns the number of bytes transferred (if zeroor positive), or else an error code.

ssize_tparport_read(structparport*port,void*buffer,size_tlen)

read a block of data from a parallel port

Parameters

structparport*port

port to read from

void*buffer

data buffer (in kernel space)

size_tlen

number of bytes of data to transfer

Description

This will read up tolen bytes ofbuffer to the portspecified, using the IEEE 1284 transfer mode most recentlynegotiated to (usingparport_negotiate()), as long as thatmode supports reverse transfers (peripheral to host).

It is the caller’s responsibility to ensure that the firstlen bytes ofbuffer are available to write to.

This function returns the number of bytes transferred (if zeroor positive), or else an error code.

longparport_set_timeout(structpardevice*dev,longinactivity)

set the inactivity timeout for a device

Parameters

structpardevice*dev

device on a port

longinactivity

inactivity timeout (in jiffies)

Description

This sets the inactivity timeout for a particular device on aport. This affects functions likeparport_wait_peripheral().The special value 0 means not to callschedule() while dealingwith this device.

The return value is the previous inactivity timeout.

Any callers ofparport_wait_event() for this device are wokenup.

int__parport_register_driver(structparport_driver*drv,structmodule*owner,constchar*mod_name)

register a parallel port device driver

Parameters

structparport_driver*drv

structure describing the driver

structmodule*owner

owner module of drv

constchar*mod_name

module name string

Description

This can be called by a parallel port device driver in orderto receive notifications about ports being found in thesystem, as well as ports no longer available.

If devmodel is true then the new device model is usedfor registration.

Thedrv structure is allocated by the caller and must not bedeallocated until after callingparport_unregister_driver().

If using the non device model:The driver’sattach() function may block. The port thatattach() is given will be valid for the duration of thecallback, but if the driver wants to take a copy of thepointer it must callparport_get_port() to do so. Callingparport_register_device() on that port will do this for you.

The driver’sdetach() function may block. The port thatdetach() is given will be valid for the duration of thecallback, but if the driver wants to take a copy of thepointer it must callparport_get_port() to do so.

Returns 0 on success. The non device model will always succeeds.but the new device model can fail and will return the error code.

voidparport_unregister_driver(structparport_driver*drv)

deregister a parallel port device driver

Parameters

structparport_driver*drv

structure describing the driver that was given toparport_register_driver()

Description

This should be called by a parallel port device driver thathas registered itself usingparport_register_driver() when itis about to be unloaded.

When it returns, the driver’sattach() routine will no longerbe called, and for each port thatattach() was called for, thedetach() routine will have been called.

All the driver’sattach() anddetach() calls are guaranteed to havefinished by the time this function returns.

structparport*parport_get_port(structparport*port)

increment a port’s reference count

Parameters

structparport*port

the port

Description

This ensures that astructparport pointer remains validuntil the matchingparport_put_port() call.

voidparport_put_port(structparport*port)

decrement a port’s reference count

Parameters

structparport*port

the port

Description

This should be called once for each call toparport_get_port(),once the port is no longer needed. When the reference count reacheszero (port is no longer used), free_port is called.

structparport*parport_register_port(unsignedlongbase,intirq,intdma,structparport_operations*ops)

register a parallel port

Parameters

unsignedlongbase

base I/O address

intirq

IRQ line

intdma

DMA channel

structparport_operations*ops

pointer to the port driver’s port operations structure

Description

When a parallel port (lowlevel) driver finds a port thatshould be made available to parallel port device drivers, itshould callparport_register_port(). Thebase,irq, anddma parameters are for the convenience of port drivers, andfor ports where they aren’t meaningful needn’t be set toanything special. They can be altered afterwards by adjustingthe relevant members of the parport structure that is returnedand represents the port. They should not be tampered withafter calling parport_announce_port, however.

If there are parallel port device drivers in the system thathave registered themselves usingparport_register_driver(),they are not told about the port at this time; that is done byparport_announce_port().

Theops structure is allocated by the caller, and must not bedeallocated before callingparport_remove_port().

If there is no memory to allocate a new parport structure,this function will returnNULL.

voidparport_announce_port(structparport*port)

tell device drivers about a parallel port

Parameters

structparport*port

parallel port to announce

Description

After a port driver has registered a parallel port withparport_register_port, and performed any necessaryinitialisation or adjustments, it should callparport_announce_port() in order to notify all device driversthat have calledparport_register_driver(). Theirattach()functions will be called, withport as the parameter.

voidparport_remove_port(structparport*port)

deregister a parallel port

Parameters

structparport*port

parallel port to deregister

Description

When a parallel port driver is forcibly unloaded, or aparallel port becomes inaccessible, the port driver must callthis function in order to deal with device drivers that stillwant to use it.

The parport structure associated with the port has itsoperations structure replaced with one containing ‘null’operations that return errors or just don’t do anything.

Any drivers that have registered themselves usingparport_register_driver() are notified that the port is nolonger accessible by having theirdetach() routines calledwithport as the parameter.

structpardevice*parport_register_dev_model(structparport*port,constchar*name,conststructpardev_cb*par_dev_cb,intid)

register a device on a parallel port

Parameters

structparport*port

port to which the device is attached

constchar*name

a name to refer to the device

conststructpardev_cb*par_dev_cb

structcontaining callbacks

intid

device number to be given to the device

Description

This function, called by parallel port device drivers,declares that a device is connected to a port, and tells thesystem all it needs to know.

Thestructpardev_cb contains pointer to callbacks. preemptioncallback function,preempt, is called when this device driverhas claimed access to the port but another device driver wantsto use it. It is given,private, as its parameter, and shouldreturn zero if it is willing for the system to release the portto another driver on its behalf. If it wants to keep control ofthe port it should return non-zero, and no action will be taken.It is good manners for the driver to try to release the port atthe earliest opportunity after its preemption callback rejects apreemption attempt. Note that if a preemption callback is happyfor preemption to go ahead, there is no need to release theport; it is done automatically. This function may not block, asit may be called from interrupt context. If the device driverdoes not support preemption,preempt can beNULL.

The wake-up (“kick”) callback function,wakeup, is called whenthe port is available to be claimed for exclusive access; thatis,parport_claim() is guaranteed to succeed when called frominside the wake-up callback function. If the driver wants toclaim the port it should do so; otherwise, it need not takeany action. This function may not block, as it may be calledfrom interrupt context. If the device driver does not want tobe explicitly invited to claim the port in this way,wakeup canbeNULL.

The interrupt handler,irq_func, is called when an interruptarrives from the parallel port. Note that if a device driverwants to use interrupts it should useparport_enable_irq(),and can also check the irq member of the parport structurerepresenting the port.

The parallel port (lowlevel) driver is the one that has calledrequest_irq() and whose interrupt handler is called first.This handler does whatever needs to be done to the hardware toacknowledge the interrupt (for PC-style ports there is nothingspecial to be done). It then tells the IEEE 1284 code aboutthe interrupt, which may involve reacting to an IEEE 1284event depending on the current IEEE 1284 phase. After this,it callsirq_func. Needless to say,irq_func will be calledfrom interrupt context, and may not block.

ThePARPORT_DEV_EXCL flag is for preventing port sharing, andso should only be used when sharing the port with other devicedrivers is impossible and would lead to incorrect behaviour.Use it sparingly! Normally,flags will be zero.

This function returns a pointer to a structure that representsthe device on the port, orNULL if there is not enough memoryto allocate space for that structure.

voidparport_unregister_device(structpardevice*dev)

deregister a device on a parallel port

Parameters

structpardevice*dev

pointer to structure representing device

Description

This undoes the effect ofparport_register_device().

structparport*parport_find_number(intnumber)

find a parallel port by number

Parameters

intnumber

parallel port number

Description

This returns the parallel port with the specified number, orNULL if there is none.

There is an implicitparport_get_port() done already; to throwaway the reference to the port thatparport_find_number()gives you, useparport_put_port().

structparport*parport_find_base(unsignedlongbase)

find a parallel port by base address

Parameters

unsignedlongbase

base I/O address

Description

This returns the parallel port with the specified baseaddress, orNULL if there is none.

There is an implicitparport_get_port() done already; to throwaway the reference to the port thatparport_find_base()gives you, useparport_put_port().

intparport_claim(structpardevice*dev)

claim access to a parallel port device

Parameters

structpardevice*dev

pointer to structure representing a device on the port

Description

This function will not block and so can be used from interruptcontext. Ifparport_claim() succeeds in claiming access tothe port it returns zero and the port is available to use. Itmay fail (returning non-zero) if the port is in use by anotherdriver and that driver is not willing to relinquish control ofthe port.

intparport_claim_or_block(structpardevice*dev)

claim access to a parallel port device

Parameters

structpardevice*dev

pointer to structure representing a device on the port

Description

This behaves likeparport_claim(), but will block if necessaryto wait for the port to be free. A return value of 1indicates that it slept; 0 means that it succeeded withoutneeding to sleep. A negative error code indicates failure.

voidparport_release(structpardevice*dev)

give up access to a parallel port device

Parameters

structpardevice*dev

pointer to structure representing parallel port device

Description

This function cannot fail, but it should not be called withoutthe port claimed. Similarly, if the port is already claimedyou should not try claiming it again.

structpardevice*parport_open(intdevnum,constchar*name)

find a device by canonical device number

Parameters

intdevnum

canonical device number

constchar*name

name to associate with the device

Description

This function is similar toparport_register_device(), exceptthat it locates a device by its number rather than by the portit is attached to.

All parameters except fordevnum are the same as forparport_register_device(). The return value is the same asforparport_register_device().

voidparport_close(structpardevice*dev)

close a device opened withparport_open()

Parameters

structpardevice*dev

device to close

Description

This is toparport_open() asparport_unregister_device() is toparport_register_device().

16x50 UART Driver

structuart_8250_port*serial8250_get_port(intline)

retrievestructuart_8250_port

Parameters

intline

serial line number

Description

This function retrievesstructuart_8250_port for the specific line.This structmustnot be used to perform a 8250 or serial core operationwhich is not accessible otherwise. Its only purpose is to make thestructaccessible to the runtime-pm callbacks for context suspend/restore.The lock assumption made here is none because runtime-pm suspend/resumecallbacks should not be invoked if there is any operation performed on theport.

voidserial8250_suspend_port(intline)

suspend one serial port

Parameters

intline

serial line number

Description

Suspend one serial port.

voidserial8250_resume_port(intline)

resume one serial port

Parameters

intline

serial line number

Description

Resume one serial port.

intserial8250_register_8250_port(conststructuart_8250_port*up)

register a serial port

Parameters

conststructuart_8250_port*up

serial port template

Description

Configure the serial port specified by the request. If theport exists and is in use, it is hung up and unregisteredfirst.

The port is then probed and if necessary the IRQ is autodetectedIf this fails an error is returned.

On success the port is ready to use and the line number is returned.

voidserial8250_unregister_port(intline)

remove a 16x50 serial port at runtime

Parameters

intline

serial line number

Description

Remove one serial port. This may not be called from interruptcontext. We hand the port back to the our control.

SeeLow Level Serial API for related APIs.

Pulse-Width Modulation (PWM)

Pulse-width modulation is a modulation technique primarily used tocontrol power supplied to electrical devices.

The PWM framework provides an abstraction for providers and consumers ofPWM signals. A controller that provides one or more PWM signals isregistered asstructpwm_chip. Providersare expected to embed this structure in a driver-specific structure.This structure contains fields that describe a particular chip.

A chip exposes one or more PWM signal sources, each of which exposed asastructpwm_device. Operations can beperformed on PWM devices to control the period, duty cycle, polarity andactive state of the signal.

Note that PWM devices are exclusive resources: they can always only beused by one consumer at a time.

enumpwm_polarity

polarity of a PWM signal

Constants

PWM_POLARITY_NORMAL

a high signal for the duration of the duty-cycle, followed by a low signal for the remainder of the pulseperiod

PWM_POLARITY_INVERSED

a low signal for the duration of the duty-cycle, followed by a high signal for the remainder of the pulseperiod

structpwm_args

board-dependent PWM arguments

Definition:

struct pwm_args {    u64 period;    enum pwm_polarity polarity;};

Members

period

reference period

polarity

reference polarity

Description

This structure describes board-dependent arguments attached to a PWMdevice. These arguments are usually retrieved from the PWM lookup table ordevice tree.

Do not confuse this with the PWM state: PWM arguments represent the initialconfiguration that users want to use on this PWM device rather than thecurrent PWM hardware state.

structpwm_waveform

description of a PWM waveform

Definition:

struct pwm_waveform {    u64 period_length_ns;    u64 duty_length_ns;    u64 duty_offset_ns;};

Members

period_length_ns

PWM period

duty_length_ns

PWM duty cycle

duty_offset_ns

offset of the rising edge from the period’s start

Description

This is a representation of a PWM waveform alternative tostructpwm_statebelow. It’s more expressive thanstructpwm_state as it contains aduty_offset_ns and so can represent offsets other than zero (with .polarity =PWM_POLARITY_NORMAL) and period - duty_cycle (.polarity =PWM_POLARITY_INVERSED).

Note there is no explicit bool for enabled. A “disabled” PWM is representedby .period_length_ns = 0. Note further that the behaviour of a “disabled” PWMis undefined. Depending on the hardware’s capabilities it might drive theactive or inactive level, go high-z or even continue to toggle.

The unit for all three members is nanoseconds.

structpwm_device

PWM channel object

Definition:

struct pwm_device {    const char *label;    unsigned long flags;    unsigned int hwpwm;    struct pwm_chip *chip;    struct pwm_args args;    struct pwm_state state;    struct pwm_state last;};

Members

label

name of the PWM device

flags

flags associated with the PWM device

hwpwm

per-chip relative index of the PWM device

chip

PWM chip providing this PWM device

args

PWM arguments

state

last applied state

last

last implemented state (for PWM_DEBUG)

voidpwm_get_state(conststructpwm_device*pwm,structpwm_state*state)

retrieve the current PWM state

Parameters

conststructpwm_device*pwm

PWM device

structpwm_state*state

state to fill with the current PWM state

Description

The returned PWM state represents the state that was applied by a previous call topwm_apply_might_sleep(). Drivers may have to slightly tweak that state before programming it tohardware. Ifpwm_apply_might_sleep() was never called, this returns either the current hardwarestate (if supported) or the default settings.

voidpwm_init_state(conststructpwm_device*pwm,structpwm_state*state)

prepare a new state to be applied withpwm_apply_might_sleep()

Parameters

conststructpwm_device*pwm

PWM device

structpwm_state*state

state to fill with the prepared PWM state

Description

This functions prepares a state that can later be tweaked and appliedto the PWM device withpwm_apply_might_sleep(). This is a convenient functionthat first retrieves the current PWM state and the replaces the periodand polarity fields with the reference values defined in pwm->args.Once the function returns, you can adjust the ->enabled and ->duty_cyclefields according to your needs before callingpwm_apply_might_sleep().

->duty_cycle is initially set to zero to avoid cases where the current->duty_cycle value exceed the pwm_args->period one, which would triggeran error if the user callspwm_apply_might_sleep() without adjusting ->duty_cyclefirst.

unsignedintpwm_get_relative_duty_cycle(conststructpwm_state*state,unsignedintscale)

Get a relative duty cycle value

Parameters

conststructpwm_state*state

PWM state to extract the duty cycle from

unsignedintscale

target scale of the relative duty cycle

Description

This functions converts the absolute duty cycle stored instate (expressedin nanosecond) into a value relative to the period.

For example if you want to get the duty_cycle expressed in percent, call:

pwm_get_state(pwm,state);duty = pwm_get_relative_duty_cycle(state, 100);

Return

rounded relative duty cycle multiplied byscale

intpwm_set_relative_duty_cycle(structpwm_state*state,unsignedintduty_cycle,unsignedintscale)

Set a relative duty cycle value

Parameters

structpwm_state*state

PWM state to fill

unsignedintduty_cycle

relative duty cycle value

unsignedintscale

scale in whichduty_cycle is expressed

Description

This functions converts a relative into an absolute duty cycle (expressedin nanoseconds), and puts the result in state->duty_cycle.

For example if you want to configure a 50% duty cycle, call:

pwm_init_state(pwm,state);pwm_set_relative_duty_cycle(state, 50, 100);pwm_apply_might_sleep(pwm,state);

Return

0 on success or-EINVAL ifduty_cycle and/orscale areinconsistent (scale == 0 orduty_cycle >scale)

structpwm_capture

PWM capture data

Definition:

struct pwm_capture {    unsigned int period;    unsigned int duty_cycle;};

Members

period

period of the PWM signal (in nanoseconds)

duty_cycle

duty cycle of the PWM signal (in nanoseconds)

structpwm_ops

PWM controller operations

Definition:

struct pwm_ops {    int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);    void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);    int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_capture *result, unsigned long timeout);    size_t sizeof_wfhw;    int (*round_waveform_tohw)(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_waveform *wf, void *wfhw);    int (*round_waveform_fromhw)(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw, struct pwm_waveform *wf);    int (*read_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, void *wfhw);    int (*write_waveform)(struct pwm_chip *chip, struct pwm_device *pwm, const void *wfhw);    int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_state *state);    int (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, struct pwm_state *state);};

Members

request

optional hook for requesting a PWM

free

optional hook for freeing a PWM

capture

capture and report PWM signal

sizeof_wfhw

size (in bytes) of driver specific waveform presentation

round_waveform_tohw

convert astructpwm_waveform to driver specific presentation

round_waveform_fromhw

convert a driver specific waveform presentation tostructpwm_waveform

read_waveform

read driver specific waveform presentation from hardware

write_waveform

write driver specific waveform presentation to hardware

apply

atomically apply a new PWM config

get_state

get the current PWM state.

structpwm_chip

abstract a PWM controller

Definition:

struct pwm_chip {    struct device dev;    struct cdev cdev;    const struct pwm_ops *ops;    struct module *owner;    unsigned int id;    unsigned int npwm;    struct pwm_device * (*of_xlate)(struct pwm_chip *chip, const struct of_phandle_args *args);    bool atomic;    struct gpio_chip gpio;    bool uses_pwmchip_alloc;    bool operational;    union {        struct mutex nonatomic_lock;        spinlock_t atomic_lock;    };    struct pwm_device pwms[] ;};

Members

dev

device providing the PWMs

cdev

structcdev for this device

ops

callbacks for this PWM controller

owner

module providing this chip

id

unique number of this PWM chip

npwm

number of PWMs controlled by this chip

of_xlate

request a PWM device given a device tree PWM specifier

atomic

can the driver’s ->apply() be called in atomic context

gpio

structgpio_chip to operate this PWM chip’s lines as GPO

uses_pwmchip_alloc

signals if pwmchip_allow was used to allocate this chip

operational

signals if the chip can be used (or is already deregistered)

{unnamed_union}

anonymous

nonatomic_lock

mutex for nonatomic chips

atomic_lock

mutex for atomic chips

pwms

array of PWM devices allocated by the framework

boolpwmchip_supports_waveform(structpwm_chip*chip)

checks if the given chip supports waveform callbacks

Parameters

structpwm_chip*chip

The pwm_chip to test

Return

true iff the pwm chip support the waveform functions likepwm_set_waveform_might_sleep() andpwm_round_waveform_might_sleep()

intpwm_config(structpwm_device*pwm,intduty_ns,intperiod_ns)

change a PWM device configuration

Parameters

structpwm_device*pwm

PWM device

intduty_ns

“on” time (in nanoseconds)

intperiod_ns

duration (in nanoseconds) of one cycle

Return

0 on success or a negative error code on failure.

intpwm_enable(structpwm_device*pwm)

start a PWM output toggling

Parameters

structpwm_device*pwm

PWM device

Return

0 on success or a negative error code on failure.

voidpwm_disable(structpwm_device*pwm)

stop a PWM output toggling

Parameters

structpwm_device*pwm

PWM device

boolpwm_might_sleep(structpwm_device*pwm)

ispwm_apply_atomic() supported?

Parameters

structpwm_device*pwm

PWM device

Return

false ifpwm_apply_atomic() can be called from atomic context.

intpwm_round_waveform_might_sleep(structpwm_device*pwm,structpwm_waveform*wf)

Query hardware capabilities Cannot be used in atomic context.

Parameters

structpwm_device*pwm

PWM device

structpwm_waveform*wf

waveform to round and output parameter

Description

Typically a given waveform cannot be implemented exactly by hardware, e.g.because hardware only supports coarse period resolution or no duty_offset.This function returns the actually implemented waveform if you passwf topwm_set_waveform_might_sleep() now.

Note however that the world doesn’t stop turning when you call it, so whendoing:

pwm_round_waveform_might_sleep(mypwm, &wf);pwm_set_waveform_might_sleep(mypwm, &wf, true);

the latter might fail, e.g. because an input clock changed its rate betweenthese two calls and the waveform determined bypwm_round_waveform_might_sleep() cannot be implemented any more.

Usually all values passed inwf are rounded down to the nearest possiblevalue (in the order period_length_ns, duty_length_ns and thenduty_offset_ns). Only if this isn’t possible, a value might grow. See thedocumentation forpwm_set_waveform_might_sleep() for a more formaldescription.

Return

0 on success, 1 if at least one value had to be rounded up or anegative errno.

Context

May sleep.

intpwm_get_waveform_might_sleep(structpwm_device*pwm,structpwm_waveform*wf)

Query hardware about current configuration Cannot be used in atomic context.

Parameters

structpwm_device*pwm

PWM device

structpwm_waveform*wf

output parameter

Description

Stores the current configuration of the PWM inwf. Note this is theequivalent ofpwm_get_state_hw() (and notpwm_get_state()) for pwm_waveform.

Return

0 on success or a negative errno

Context

May sleep.

intpwm_set_waveform_might_sleep(structpwm_device*pwm,conststructpwm_waveform*wf,boolexact)

Apply a new waveform Cannot be used in atomic context.

Parameters

structpwm_device*pwm

PWM device

conststructpwm_waveform*wf

The waveform to apply

boolexact

If true no rounding is allowed

Description

Typically a requested waveform cannot be implemented exactly, e.g. becauseyou requested .period_length_ns = 100 ns, but the hardware can only setperiods that are a multiple of 8.5 ns. With that hardware passingexact =true results inpwm_set_waveform_might_sleep() failing and returning -EDOM.Ifexact = false you get a period of 93.5 ns (i.e. the biggest period notbigger than the requested value).Note that even withexact = true, some rounding by less than 1 ns ispossible/needed. In the above example requesting .period_length_ns = 94 andexact = true, you get the hardware configured with period = 93.5 ns.

Let C be the set of possible hardware configurations for a given PWM device,consisting of tuples (p, d, o) where p is the period length, d is the dutylength and o the duty offset.

The following algorithm is implemented to pick the hardware setting(p, d, o) ∈ C for a given request (p’, d’, o’) withexact = false:

p = max( { ṗ | ∃ ḋ, ȯ : (ṗ, ḋ, ȯ) ∈ C ∧ ṗ ≤ p' } ∪ { min({ ṗ | ∃ ḋ, ȯ : (ṗ, ḋ, ȯ) ∈ C }) })d = max( { ḋ | ∃ ȯ : (p, ḋ, ȯ) ∈ C ∧ ḋ ≤ d' } ∪ { min({ ḋ | ∃ ȯ : (p, ḋ, ȯ) ∈ C }) })o = max( { ȯ | (p, d, ȯ) ∈ C ∧ ȯ ≤ o' } ∪ { min({ ȯ | (p, d, ȯ) ∈ C }) })

In words: The chosen period length is the maximal possible period length notbigger than the requested period length and if that doesn’t exist, theminimal period length. The chosen duty length is the maximal possible dutylength that is compatible with the chosen period length and isn’t bigger thanthe requested duty length. Again if such a value doesn’t exist, the minimalduty length compatible with the chosen period is picked. After that the dutyoffset compatible with the chosen period and duty length is chosen in thesame way.

Return

0 on success, -EDOM if setting failed due to the exact waveform notbeing possible (ifexact), or a different negative errno on failure.

Context

May sleep.

intpwm_apply_might_sleep(structpwm_device*pwm,conststructpwm_state*state)

atomically apply a new state to a PWM device Cannot be used in atomic context.

Parameters

structpwm_device*pwm

PWM device

conststructpwm_state*state

new state to apply

Return

0 on success, or a negative errno

Context

May sleep.

intpwm_apply_atomic(structpwm_device*pwm,conststructpwm_state*state)

apply a new state to a PWM device from atomic context Not all PWM devices support this function, check withpwm_might_sleep().

Parameters

structpwm_device*pwm

PWM device

conststructpwm_state*state

new state to apply

Return

0 on success, or a negative errno

Context

Any

intpwm_get_state_hw(structpwm_device*pwm,structpwm_state*state)

get the current PWM state from hardware

Parameters

structpwm_device*pwm

PWM device

structpwm_state*state

state to fill with the current PWM state

Description

Similar topwm_get_state() but reads the current PWM state from hardwareinstead of the requested state.

Return

0 on success or a negative error code on failure.

Context

May sleep.

intpwm_adjust_config(structpwm_device*pwm)

adjust the current PWM config to the PWM arguments

Parameters

structpwm_device*pwm

PWM device

Description

This function will adjust the PWM config to the PWM arguments providedby the DT or PWM lookup table. This is particularly useful to adaptthe bootloader config to the Linux one.

Return

0 on success or a negative error code on failure.

Context

May sleep.

structpwm_device*pwm_get(structdevice*dev,constchar*con_id)

look up and request a PWM device

Parameters

structdevice*dev

device for PWM consumer

constchar*con_id

consumer name

Description

Lookup is first attempted using DT. If the device was not instantiated froma device tree, a PWM chip and a relative index is looked up via a tablesupplied by board setup code (seepwm_add_table()).

Once a PWM chip has been found the specified PWM device will be requestedand is ready to be used.

Return

A pointer to the requested PWM device or anERR_PTR()-encodederror code on failure.

voidpwm_put(structpwm_device*pwm)

release a PWM device

Parameters

structpwm_device*pwm

PWM device

structpwm_device*devm_pwm_get(structdevice*dev,constchar*con_id)

resource managedpwm_get()

Parameters

structdevice*dev

device for PWM consumer

constchar*con_id

consumer name

Description

This function performs likepwm_get() but the acquired PWM device willautomatically be released on driver detach.

Return

A pointer to the requested PWM device or anERR_PTR()-encodederror code on failure.

structpwm_device*devm_fwnode_pwm_get(structdevice*dev,structfwnode_handle*fwnode,constchar*con_id)

request a resource managed PWM from firmware node

Parameters

structdevice*dev

device for PWM consumer

structfwnode_handle*fwnode

firmware node to get the PWM from

constchar*con_id

consumer name

Description

Returns the PWM device parsed from the firmware node. Seeof_pwm_get() andacpi_pwm_get() for a detailed description.

Return

A pointer to the requested PWM device or anERR_PTR()-encodederror code on failure.

int__pwmchip_add(structpwm_chip*chip,structmodule*owner)

register a new PWM chip

Parameters

structpwm_chip*chip

the PWM chip to add

structmodule*owner

reference to the module providing the chip.

Description

Register a new PWM chip.owner is supposed to be THIS_MODULE, use thepwmchip_add wrapper to do this right.

Return

0 on success or a negative error code on failure.

voidpwmchip_remove(structpwm_chip*chip)

remove a PWM chip

Parameters

structpwm_chip*chip

the PWM chip to remove

Description

Removes a PWM chip.