qemu/include/hw/misc/npcm7xx_pwm.h
Hao Wu 1e943c586a hw/misc: Add a PWM module for NPCM7XX
The PWM module is part of NPCM7XX module. Each NPCM7XX module has two
identical PWM modules. Each module contains 4 PWM entries. Each PWM has
two outputs: frequency and duty_cycle. Both are computed using inputs
from software side.

This module does not model detail pulse signals since it is expensive.
It also does not model interrupts and watchdogs that are dependant on
the detail models. The interfaces for these are left in the module so
that anyone in need for these functionalities can implement on their
own.

The user can read the duty cycle and frequency using qom-get command.

Reviewed-by: Havard Skinnemoen <hskinnemoen@google.com>
Reviewed-by: Tyrone Ting <kfting@nuvoton.com>
Signed-off-by: Hao Wu <wuhaotsh@google.com>
Message-id: 20210108190945.949196-5-wuhaotsh@google.com
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
2021-01-12 21:19:02 +00:00

106 lines
3.0 KiB
C

/*
* Nuvoton NPCM7xx PWM Module
*
* Copyright 2020 Google LLC
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#ifndef NPCM7XX_PWM_H
#define NPCM7XX_PWM_H
#include "hw/clock.h"
#include "hw/sysbus.h"
#include "hw/irq.h"
/* Each PWM module holds 4 PWM channels. */
#define NPCM7XX_PWM_PER_MODULE 4
/*
* Number of registers in one pwm module. Don't change this without increasing
* the version_id in vmstate.
*/
#define NPCM7XX_PWM_NR_REGS (0x54 / sizeof(uint32_t))
/*
* The maximum duty values. Each duty unit represents 1/NPCM7XX_PWM_MAX_DUTY
* cycles. For example, if NPCM7XX_PWM_MAX_DUTY=1,000,000 and a PWM has a duty
* value of 100,000 the duty cycle for that PWM is 10%.
*/
#define NPCM7XX_PWM_MAX_DUTY 1000000
typedef struct NPCM7xxPWMState NPCM7xxPWMState;
/**
* struct NPCM7xxPWM - The state of a single PWM channel.
* @module: The PWM module that contains this channel.
* @irq: GIC interrupt line to fire on expiration if enabled.
* @running: Whether this PWM channel is generating output.
* @inverted: Whether this PWM channel is inverted.
* @index: The index of this PWM channel.
* @cnr: The counter register.
* @cmr: The comparator register.
* @pdr: The data register.
* @pwdr: The watchdog register.
* @freq: The frequency of this PWM channel.
* @duty: The duty cycle of this PWM channel. One unit represents
* 1/NPCM7XX_MAX_DUTY cycles.
*/
typedef struct NPCM7xxPWM {
NPCM7xxPWMState *module;
qemu_irq irq;
bool running;
bool inverted;
uint8_t index;
uint32_t cnr;
uint32_t cmr;
uint32_t pdr;
uint32_t pwdr;
uint32_t freq;
uint32_t duty;
} NPCM7xxPWM;
/**
* struct NPCM7xxPWMState - Pulse Width Modulation device state.
* @parent: System bus device.
* @iomem: Memory region through which registers are accessed.
* @clock: The PWM clock.
* @pwm: The PWM channels owned by this module.
* @ppr: The prescaler register.
* @csr: The clock selector register.
* @pcr: The control register.
* @pier: The interrupt enable register.
* @piir: The interrupt indication register.
*/
struct NPCM7xxPWMState {
SysBusDevice parent;
MemoryRegion iomem;
Clock *clock;
NPCM7xxPWM pwm[NPCM7XX_PWM_PER_MODULE];
uint32_t ppr;
uint32_t csr;
uint32_t pcr;
uint32_t pier;
uint32_t piir;
};
#define TYPE_NPCM7XX_PWM "npcm7xx-pwm"
#define NPCM7XX_PWM(obj) \
OBJECT_CHECK(NPCM7xxPWMState, (obj), TYPE_NPCM7XX_PWM)
#endif /* NPCM7XX_PWM_H */